Storage模块管理应用本地数据存储区,用于应用数据的保存和读取。应用本地数据与localStorage、sessionStorage的区别在于数据有效域不同,前者可在应用内跨域操作,数据存储期是持久化的,并且没有容量限制。通过plus.storage可获取应用本地数据管理对象。
方法:
- clear: 清除存储数据
- clearAsync: 异步清除存储数据
- getAllKeys: 获取所有键名
- getAllKeysAsync: 异步获取所有键名
- getLength: 获取存储数据的个数
- getItem: 获取存储的键值
- getItemAsync: 异步获取存储的键值
- key: 获取键值对中指定索引值的key值
- setItem: 存储键值
- setItemAsync: 异步存储键值
- removeItem: 删除键值
- removeItemAsync: 异步删除键值
回调方法:
- StorageSuccessCallback: 本地数据存储操作成功的回调函数
- StorageErrorCallback: 本地数据存储操作失败的回调函数
权限:
5+功能模块(permissions)
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#ffffff">{</span>
<span style="color:#87ceeb">// ...</span>
<span style="color:#ffa0a0">"permissions"</span><span style="color:#ffffff">:{</span>
<span style="color:#87ceeb">// ...</span>
<span style="color:#ffa0a0">"Storage"</span><span style="color:#ffffff">:</span> <span style="color:#ffffff">{</span>
<span style="color:#ffa0a0">"description"</span><span style="color:#ffffff">:</span> <span style="color:#ffa0a0">"本地存储,访问应用本地存储数据"</span>
<span style="color:#ffffff">}</span>
<span style="color:#ffffff">}</span>
<span style="color:#ffffff">}</span>
</code></span></span>
clear
清除存储数据
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">clear</span><span style="color:#ffffff">();</span>
</code></span></span>
说明:
清除存储的所有键名及键值。
参数:
无
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 清除所有存储数据</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> clearStorage</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">clear</span><span style="color:#ffffff">();</span>
<span style="color:#ffffff">}</span>
</code></span></span>
clearAsync
异步清除存储数据
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">clearAsync</span><span style="color:#ffffff">(</span><span style="color:#ffffff">successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
异步清除存储的所有键名及键值。 注意:HBuilderX2.6.6+版本支持。
参数:
- successCB: ( StorageSuccessCallback ) 可选 清除操作成功回调
回调参数不包含属性。
- errorCB: ( StorageErrorCallback ) 可选 清除操作失败回调
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 异步清除存储数据</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> clearAsync</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">clearAsync</span><span style="color:#ffffff">(</span><span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"clearAsync success"</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"clearAsync failed: "</span><span style="color:#ffffff">+</span><span style="color:#ffffff">JSON</span><span style="color:#ffffff">.</span><span style="color:#ffffff">stringify</span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">));</span>
<span style="color:#ffffff">});</span>
<span style="color:#ffffff">}</span>
</code></span></span>
getAllKeys
获取所有键名
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> keys </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getAllKeys</span><span style="color:#ffffff">();</span>
</code></span></span>
说明:
获取存储的所有键名,如果没有存储数据则返回空数组。 注意:HBuilderX2.6.6+版本支持。
参数:
无
返回值:
Array[String] : 所有键名字符串数组。
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 获取所有键名</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> getAllKeys</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> keys </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getAllKeys</span><span style="color:#ffffff">();</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'keys length: "+keys.length);
}
</span></code></span></span>
getAllKeysAsync
异步获取所有键名
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getAllKeysAsync</span><span style="color:#ffffff">(</span><span style="color:#ffffff">successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
异步获取存储的所有键名,如果没有存储数据则返回空数组。 注意:HBuilderX2.6.6+版本支持。
参数:
- successCB: ( StorageSuccessCallback ) 可选 获取成功回调
回调参数包含以下属性:
- keys - 获取到所有键名的数组
- errorCB: ( StorageErrorCallback ) 可选 获取失败回调
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 异步获取所有键名</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> getAllKeysAsync</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getAllKeysAsync</span><span style="color:#ffffff">(</span><span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> keys </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">keys</span><span style="color:#ffffff">;</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">'keys length: "+keys.length);
}, function(e){
console.log("clearAsync failed: "+JSON.stringify(e));
});
}
</span></code></span></span>
getLength
获取存储数据的个数
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> foo </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getLength</span><span style="color:#ffffff">();</span>
</code></span></span>
参数:
无
返回值:
Number : 键值对的个数
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 获取存储数据的个数</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> storageCount</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span>
<span style="color:#f0e68c"><strong>return</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getLength</span><span style="color:#ffffff">();</span>
<span style="color:#ffffff">}</span>
</code></span></span>
getItem
获取存储的键值
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> foo </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getItem</span><span style="color:#ffffff">(</span><span style="color:#ffffff">key</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
通过键名获取对应存储的键值。
参数:
- key: ( String ) 必选
键名
返回值:
String : 键名对应的键值,如果不存在则返回null。
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 获取存储的键值</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> getStorage</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span>
<span style="color:#f0e68c"><strong>return</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getItem</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"test"</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span>
</code></span></span>
getItemAsync
异步获取存储的键值
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getItemAsync</span><span style="color:#ffffff">(</span><span style="color:#ffffff">key</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
通过键名异步获取对应存储的键值,获取成功通过successCB回调返回键值,失败则通过errorCB返回错误。 注意:HBuilderX2.6.6+版本支持。
参数:
- key: ( String ) 必选
键名
- successCB: ( StorageSuccessCallback ) 可选 获取键值成功回调
回调参数包含以下属性:
- data - 获取到键名对应的键值
- errorCB: ( StorageErrorCallback ) 可选 获取键值失败回调
如果获取的键名不存在,则会触发失败回调。
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 异步获取存储的键值</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> getStorageAsync</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getItemAsync</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"test"</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> data </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> e</span><span style="color:#ffffff">.</span><span style="color:#ffffff">data</span><span style="color:#ffffff">;</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"getItemAsync success: "</span><span style="color:#ffffff">+</span><span style="color:#ffffff">data</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"getItemAsync failed: "</span><span style="color:#ffffff">+</span><span style="color:#ffffff">JSON</span><span style="color:#ffffff">.</span><span style="color:#ffffff">stringify</span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">));</span>
<span style="color:#ffffff">});</span>
<span style="color:#ffffff">}</span>
</code></span></span>
key
获取键值对中指定索引值的key值
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> foo </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">key</span><span style="color:#ffffff">(</span><span style="color:#ffffff">index</span><span style="color:#ffffff">);</span>
</code></span></span>
参数:
- index: ( Number ) 必选 存储键值的索引
返回值:
String : 指定索引值的key值,如果不存在则返回null。
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 获取键值对中指定索引值的key值</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> enumValueByIndex</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> keyNames</span><span style="color:#ffffff">=[];</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> values</span><span style="color:#ffffff">=[];</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> numKeys</span><span style="color:#ffffff">=</span><span style="color:#ffffff">plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getLength</span><span style="color:#ffffff">();</span>
<span style="color:#f0e68c"><strong>for</strong></span><span style="color:#ffffff">(</span><span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> i</span><span style="color:#ffffff">=</span><span style="color:#cd5c5c">0</span><span style="color:#ffffff">;</span><span style="color:#ffffff"> i</span><span style="color:#ffffff"><</span><span style="color:#ffffff">numKeys</span><span style="color:#ffffff">;</span><span style="color:#ffffff"> i</span><span style="color:#ffffff">++)</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
keyNames</span><span style="color:#ffffff">[</span><span style="color:#ffffff">i</span><span style="color:#ffffff">]</span> <span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">key</span><span style="color:#ffffff">(</span><span style="color:#ffffff">i</span><span style="color:#ffffff">);</span><span style="color:#ffffff">
values</span><span style="color:#ffffff">[</span><span style="color:#ffffff">i</span><span style="color:#ffffff">]</span> <span style="color:#ffffff">=</span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">getItem</span><span style="color:#ffffff">(</span><span style="color:#ffffff">keyNames</span><span style="color:#ffffff">[</span><span style="color:#ffffff">i</span><span style="color:#ffffff">]);</span>
<span style="color:#ffffff">}</span>
<span style="color:#ffffff">}</span>
</code></span></span>
setItem
存储键值
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">setItem</span><span style="color:#ffffff">(</span><span style="color:#ffffff">key</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> value</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
如果设置的键在数据存储中已经存在,则更新存储的键值。 存储的键和值没有容量限制,但过多的数据量会导致效率降低,建议单个键值数据不要超过10Kb。
参数:
- key: ( String ) 必选
存储的键名
- value: ( String ) 必选
存储的键值
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 修改或添加键值(key-value)对数据到应用数据存储中</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> setStorage</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">setItem</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"test"</span><span style="color:#ffffff">,</span><span style="color:#ffa0a0">"test with hbuilder!"</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span>
</code></span></span>
setItemAsync
异步存储键值
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">setItemAsync</span><span style="color:#ffffff">(</span><span style="color:#ffffff">key</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> value</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
如果设置的键在数据存储中已经存在,则更新存储的键值。 存储的键和值没有容量限制,但过多的数据量会导致效率降低, 异步存储不阻塞js运行,但也不应该传入过大数据,建议单个键值数据不要超过100Kb。 注意:HBuilderX2.6.6+版本支持。
参数:
- key: ( String ) 必选
存储的键名
- value: ( String ) 必选
存储的键值
- successCB: ( StorageSuccessCallback ) 可选 存储键值成功回调
回调参数不包含属性。
- errorCB: ( StorageErrorCallback ) 可选 存储键值失败回调
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 异步存储键值</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> setStorageAsync</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">setItemAsync</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"test"</span><span style="color:#ffffff">,</span> <span style="color:#ffa0a0">"test with HBuilderX"</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"setItemAsync success"</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"setItemAsync failed: "</span><span style="color:#ffffff">+</span><span style="color:#ffffff">JSON</span><span style="color:#ffffff">.</span><span style="color:#ffffff">stringify</span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">));</span>
<span style="color:#ffffff">});</span>
<span style="color:#ffffff">}</span>
</code></span></span>
removeItem
删除键值
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">removeItem</span><span style="color:#ffffff">(</span><span style="color:#ffffff">key</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
通过键名删除对应存储的键名及键值。
参数:
- key: ( String ) 必选 键名
要删除的键名。
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 删除键值</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> removeStorage</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">removeItem</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"test"</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">}</span>
</code></span></span>
removeItemAsync
异步删除键值
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">removeItemAsync</span><span style="color:#ffffff">(</span><span style="color:#ffffff">key</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> successCB</span><span style="color:#ffffff">,</span><span style="color:#ffffff"> errorCB</span><span style="color:#ffffff">);</span>
</code></span></span>
说明:
通过键名异步删除对应存储的键名及键值。 注意:HBuilderX2.6.6+版本支持。
参数:
- key: ( String ) 必选
删除的键名
- successCB: ( StorageSuccessCallback ) 可选 删除操作成功回调
回调参数不包含属性。
- errorCB: ( StorageErrorCallback ) 可选 删除操作失败回调
返回值:
void : 无
示例:
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#87ceeb">// 异步删除键值</span>
<span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff"> removeItemAsync</span><span style="color:#ffffff">()</span> <span style="color:#ffffff">{</span><span style="color:#ffffff">
plus</span><span style="color:#ffffff">.</span><span style="color:#ffffff">storage</span><span style="color:#ffffff">.</span><span style="color:#ffffff">removeItemAsync</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"test"</span><span style="color:#ffffff">,</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"removeItemAsync success"</span><span style="color:#ffffff">);</span>
<span style="color:#ffffff">},</span> <span style="color:#f0e68c"><strong>function</strong></span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">){</span><span style="color:#ffffff">
console</span><span style="color:#ffffff">.</span><span style="color:#ffffff">log</span><span style="color:#ffffff">(</span><span style="color:#ffa0a0">"removeItemAsync failed: "</span><span style="color:#ffffff">+</span><span style="color:#ffffff">JSON</span><span style="color:#ffffff">.</span><span style="color:#ffffff">stringify</span><span style="color:#ffffff">(</span><span style="color:#ffffff">e</span><span style="color:#ffffff">));</span>
<span style="color:#ffffff">});</span>
<span style="color:#ffffff">}</span>
</code></span></span>
StorageSuccessCallback
本地数据存储操作成功的回调函数
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> onSuccess</span><span style="color:#ffffff">(</span> <span style="color:#f0e68c"><strong>event</strong></span> <span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span>
<span style="color:#87ceeb">// get event data</span>
<span style="color:#ffffff">}</span>
</code></span></span>
说明:
不同接口触发的成功回调参数event包含的属性存在差异,具体参考对应的接口描述说明。
参数:
- event: ( Object ) 可选 回调参数
回调参数包含的属性由调用接口决定,具体参考对应的接口描述说明。
返回值:
void : 无
StorageErrorCallback
本地数据存储操作失败的回调函数
<span style="background-color:#333333"><span style="color:#474747"><code>
<span style="color:#f0e68c"><strong>function</strong></span> <span style="color:#f0e68c"><strong>void</strong></span><span style="color:#ffffff"> onError</span><span style="color:#ffffff">(</span><span style="color:#98fb98">Exception</span><span style="color:#ffffff"> error</span><span style="color:#ffffff">)</span> <span style="color:#ffffff">{</span>
<span style="color:#87ceeb">// Handle error</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> code </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> error</span><span style="color:#ffffff">.</span><span style="color:#ffffff">code</span><span style="color:#ffffff">;</span> <span style="color:#87ceeb">// 错误编码</span>
<span style="color:#f0e68c"><strong>var</strong></span><span style="color:#ffffff"> message </span><span style="color:#ffffff">=</span><span style="color:#ffffff"> error</span><span style="color:#ffffff">.</span><span style="color:#ffffff">message</span><span style="color:#ffffff">;</span> <span style="color:#87ceeb">// 错误描述信息</span>
<span style="color:#ffffff">}</span>
</code></span></span>
说明:
包括以下错误:
- -1 - 内部错误
- -2 - 存储已超上限 无法正常存储
- -3 - 没有找到相同key的存储记录
参数:
- error: ( Exception ) 必选 错误回调信息
可通过error.code(Number类型)获取错误编码; 可通过error.message(String类型)获取错误描述信息。
返回值:
void : 无