🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# store 客户端存储 <https://cdn.bootcss.com/store.js/1.3.20/store.min.js> // 存储 'username' 的值为 'marcus' store.set('username', '123'); ``` <pre class="calibre16">``` <span class="token6">// 移除 'username' 字段</span> store<span class="token2">.</span><span class="token1">remove</span><span class="token2">(</span><span class="token4">'username'</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 获取 'username'</span> var aaa <span class="token">=</span> store<span class="token2">.</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'username'</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 清除所有本地存储</span> store<span class="token2">.</span><span class="token1">clear</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">// 存储对象 - 自动调用 JSON.stringify</span> store<span class="token2">.</span><span class="token1">set</span><span class="token2">(</span><span class="token4">'user'</span><span class="token2">,</span> <span class="token2">{</span> name<span class="token2">:</span> <span class="token4">'marcus'</span><span class="token2">,</span> likes<span class="token2">:</span> <span class="token4">'javascript'</span> <span class="token2">}</span><span class="token2">)</span> <span class="token6">// 获取存储的对象 - 自动执行 JSON.parse</span> var user <span class="token">=</span> store<span class="token2">.</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'user'</span><span class="token2">)</span> console<span class="token2">.</span><span class="token1">log</span><span class="token2">(</span>user<span class="token2">.</span>name <span class="token">+</span> <span class="token4">' likes '</span> <span class="token">+</span> user<span class="token2">.</span>likes<span class="token2">)</span> <span class="token6">// 从所有存储中获取值</span> store<span class="token2">.</span><span class="token1">getAll</span><span class="token2">(</span><span class="token2">)</span><span class="token2">.</span>user<span class="token2">.</span>name <span class="token">==</span> <span class="token4">'marcus'</span> <span class="token6">// 遍历所有存储</span> store<span class="token2">.</span><span class="token1">forEach</span><span class="token2">(</span><span class="token5">function</span><span class="token2">(</span>key<span class="token2">,</span> val<span class="token2">)</span> <span class="token2">{</span> console<span class="token2">.</span><span class="token1">log</span><span class="token2">(</span>key<span class="token2">,</span> <span class="token4">'=='</span><span class="token2">,</span> val<span class="token2">)</span> <span class="token2">}</span><span class="token2">)</span> ``` ```