swan.setStorageSync

解释:将数据存储在本地缓存中指定的 key 中。如果之前存在同名 key,会覆盖掉原来该 key 对应的内容。这是一个同步接口。

方法参数

String key, Object/String/Number/Array/Boolean data

key 参数说明

本地缓存中的指定的 key

data 参数说明

需要存储的内容

示例

跳转编辑工具

在开发者工具中打开

在 WEB IDE 中打开

扫码体验

代码示例

百度智能小程序

请使用百度APP扫码

图片示例

swan.setStorageSync - 图2

代码示例

  • SWAN
  • JS
  1. <view class="wrap">
  2. <view class="card-area">
  3. <view class="list-area border-bottom">
  4. <label class="list-item-key-4">key</label>
  5. <input class="list-item-value" bindfocus="keyFocus" bindinput="keyInput" type="text" value="{{key}}" placeholder="请输入key" />
  6. </view>
  7. <view class="list-area border-bottom">
  8. <label class="list-item-key-4">value</label>
  9. <input class="list-item-value" bindfocus="valueFocus" bindinput="valueInput" type="text" value="{{value}}" placeholder="请输入value" />
  10. </view>
  11. <view>
  12. <button bindtap="setStorageSync" type="primary" hover-stop-propagation="true">存储数据</button>
  13. <button bindtap="getStorageSync" type="primary" hover-stop-propagation="true" disabled="{{disabled}}">读取数据</button>
  14. </view>
  15. </view>
  16. </view>

Bug & Tip

  • Tip:基础库 3.140.1 之前,无法判断接口是否调用失败。
  • Tip:基础库 3.140.1 及以后,接口调用失败时会返回一个标准的Error对象,可通过instanceof来判断接口是否调用失败。

错误码

Android

错误码说明

201

解析失败,请检查调起协议是否合法

1001

执行失败

iOS

错误码说明

202

解析失败,请检查参数是否正确

1003

超过最大存储文件大小