🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Locker实现localStrage的简便方法 \[ # localStorage插件lockr ##### Lockr.prefix - String 设置一个前缀,将被添加到每个键被锁的字符串值 ``` <pre class="calibre13">``` Lockr<span class="token1">.</span>prefix <span class="token">=</span> <span class="token4">'lockr'</span><span class="token1">;</span> Lockr<span class="token1">.</span><span class="token2">set</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">,</span> <span class="token4">'Coyote'</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// Saved as string</span> localStorage<span class="token1">.</span><span class="token3">getItem</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">></span> <span class="token2">null</span> localStorage<span class="token1">.</span><span class="token3">getItem</span><span class="token1">(</span><span class="token4">'lockr_username'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">></span> <span class="token1">{</span><span class="token4">"data"</span><span class="token1">:</span><span class="token5">123</span><span class="token1">}</span> ``` ``` ##### Lockr.set - 参数: \[ key, value \] {String, Number, Array or Object} 设置一个指定的值,可以是任意类型 ``` <pre class="calibre13">``` Lockr<span class="token1">.</span><span class="token2">set</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">,</span> <span class="token4">'Coyote'</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 保存字符串</span> Lockr<span class="token1">.</span><span class="token2">set</span><span class="token1">(</span><span class="token4">'user_id'</span><span class="token1">,</span> <span class="token5">12345</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// 保存整型</span> Lockr<span class="token1">.</span><span class="token2">set</span><span class="token1">(</span><span class="token4">'users'</span><span class="token1">,</span> <span class="token1">[</span><span class="token1">{</span>name<span class="token1">:</span> <span class="token4">'John Doe'</span><span class="token1">,</span> age<span class="token1">:</span> <span class="token5">18</span><span class="token1">}</span><span class="token1">,</span> <span class="token1">{</span>name<span class="token1">:</span> <span class="token4">'Jane Doe'</span><span class="token1">,</span> age<span class="token1">:</span> <span class="token5">19</span><span class="token1">}</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//保存对象</span> ``` ``` ##### Lockr.get - 参数: \[ key or hash\_key, default value \] 通过给定的键返回被保存的值,如果指定的键是null或undefined则会返回一个默认值 ``` <pre class="calibre13">``` Lockr<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">></span> <span class="token4">"Coyote"</span> Lockr<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'user_id'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">></span> <span class="token5">12345</span> Lockr<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'users'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">></span> <span class="token1">[</span><span class="token1">{</span>name<span class="token1">:</span> <span class="token4">'John Doe'</span><span class="token1">,</span> age<span class="token1">:</span> <span class="token5">18</span><span class="token1">}</span><span class="token1">,</span> <span class="token1">{</span>name<span class="token1">:</span> <span class="token4">'Jane Doe'</span><span class="token1">,</span> age<span class="token1">:</span> <span class="token5">19</span><span class="token1">}</span><span class="token1">]</span> Lockr<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'score'</span><span class="token1">,</span> <span class="token5">0</span><span class="token1">)</span><span class="token1">:</span> <span class="token">></span> <span class="token5">0</span> Lockr<span class="token1">.</span><span class="token2">set</span><span class="token1">(</span><span class="token4">'score'</span><span class="token1">,</span> <span class="token5">3</span><span class="token1">)</span><span class="token1">:</span> Lockr<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'score'</span><span class="token1">,</span> <span class="token5">0</span><span class="token1">)</span><span class="token1">:</span> <span class="token">></span> <span class="token5">3</span> ``` ``` ##### Lockr.rm - 参数: \[ key \] {String} 完全删除指定的键值对 ``` <pre class="calibre13">``` Lockr<span class="token1">.</span><span class="token2">set</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">,</span> <span class="token4">'Coyote'</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// Saved as string</span> Lockr<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">></span> <span class="token4">"Coyote"</span> Lockr<span class="token1">.</span><span class="token3">rm</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">)</span><span class="token1">;</span> Lockr<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">)</span><span class="token1">;</span> <span class="token">></span> undefined ``` ``` ##### Lockr.sadd - 参数 key, value \]{String, Number, Array or Object} 追加一个值在之前的基础上面 ``` <pre class="calibre13">``` Lockr<span class="token1">.</span><span class="token3">sadd</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// [1]</span> Lockr<span class="token1">.</span><span class="token3">sadd</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// [1, 2]</span> Lockr<span class="token1">.</span><span class="token3">sadd</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// [1, 2]</span> ``` ``` ##### sismember判断指定键是否存在那个值 ``` <pre class="calibre13">``` Lockr<span class="token1">.</span><span class="token3">sadd</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> Lockr<span class="token1">.</span><span class="token3">sismember</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// true</span> Lockr<span class="token1">.</span><span class="token3">sismember</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// false</span> ``` ``` ##### srem移除指定键下的那个值 ``` <pre class="calibre13">``` Lockr<span class="token1">.</span><span class="token3">sadd</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> Lockr<span class="token1">.</span><span class="token3">sadd</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">)</span><span class="token1">;</span> Lockr<span class="token1">.</span><span class="token3">srem</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">,</span> <span class="token5">1</span><span class="token1">)</span><span class="token1">;</span> Lockr<span class="token1">.</span><span class="token3">smembers</span><span class="token1">(</span><span class="token4">"wat"</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// [2]</span> ``` ``` ##### getAll获取本地存储中所有的键值对 ``` <pre class="calibre14">``` Lockr<span class="token1">.</span><span class="token3">getAll</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token">></span> <span class="token1">[</span><span class="token4">"Coyote"</span><span class="token1">,</span> <span class="token5">12345</span><span class="token1">,</span> <span class="token1">[</span><span class="token1">{</span>name<span class="token1">:</span> <span class="token4">'John Doe'</span><span class="token1">,</span> age<span class="token1">:</span> <span class="token5">18</span><span class="token1">}</span><span class="token1">,</span> <span class="token1">{</span>name<span class="token1">:</span> <span class="token4">'Jane Doe'</span><span class="token1">,</span> age<span class="token1">:</span> <span class="token5">19</span><span class="token1">}</span><span class="token1">]</span><span class="token1">]</span> ``` ```