{"id":49,"date":"2021-01-13T08:57:24","date_gmt":"2021-01-13T00:57:24","guid":{"rendered":"http:\/\/mengstudy.com\/?p=49"},"modified":"2021-01-13T08:57:24","modified_gmt":"2021-01-13T00:57:24","slug":"vue%e9%a1%b9%e7%9b%ae%e9%9b%86%e6%88%90vuex-persistedstate","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=49","title":{"rendered":"Vue\u9879\u76ee\u96c6\u6210vuex-persistedstate"},"content":{"rendered":"<p>Vue\u9879\u76ee\u4e2d\u4f7f\u7528Vuex\u4f5c\u4e3a\u72b6\u6001\u7ba1\u7406\u5df2\u7ecf\u662f\u6bd4\u8f83\u901a\u7528\u7684\u505a\u6cd5\u4e86\uff0c\u65b0\u5efaVue\u9879\u76ee\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u9009\u62e9\u96c6\u6210Vuex\uff0c\u5176\u5b9eVuex\u672c\u8d28\u4e0a\u7c7b\u4f3c\u5168\u5c40\u7684\u53d8\u91cf\u5b58\u50a8\uff0c\u65b9\u4fbf\u5728\u6240\u6709Vue\u7ec4\u4ef6\u4e2d\u5171\u4eab\uff0c\u800c\u4e14\u4e5f\u53ef\u4ee5\u52a8\u6001\u6539\u53d8\u72b6\u6001\u3002<\/p>\n<p>\u5728\u5355\u9875\u5e94\u7528\u4e2dVue\u9879\u76ee\u96c6\u6210Vuex\u4e5f\u5c31\u8db3\u591f\u57fa\u672c\u4f7f\u7528\u4e86\uff0c\u4f46\u662f\u5237\u65b0\u9875\u9762\u7684\u65f6\u5019\u6570\u636e\u90fd\u4f1a\u88ab\u6e05\u7a7a\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u8fd9\u4e9b\u72b6\u6001\u80fd\u4fdd\u5b58\u4e0b\u6765\uff0c\u6bd4\u5982\u767b\u5f55\u540e\u7684\u7528\u6237\u4fe1\u606f\u3001AccessToken\u3001\u4e3b\u9898\u914d\u7f6e\u7b49\u3002<\/p>\n<p>\u8fd9\u91cc\u5c31\u9700\u8981vuex-persistedstate\uff0c\u4ed6\u53ef\u4ee5\u628a\u6570\u636e\u4fdd\u5b58\u5230localStorage\u6216\u8005Cookie<\/p>\n<h3>\u5b89\u88c5vuex-persistedstate<\/h3>\n<p>GitHub\u5730\u5740\uff1ahttps:\/\/github.com\/robinvdvleuten\/vuex-persistedstate<\/p>\n<p>\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"language-shell line-numbers\">npm install vuex-persistedstate --save\n<\/code><\/pre>\n<p>\u5b89\u88c5\u597d\u4e4b\u540e\u5c31\u8981\u505a\u4e00\u4e9b\u914d\u7f6e\u4e86<\/p>\n<h3>\u914d\u7f6evuex-persistedstate<\/h3>\n<p>\u5728<code>store\/index.js<\/code>\u4e2d\u4f7f\u7528<\/p>\n<pre><code class=\"language-js line-numbers\">import Vue from 'vue'\nimport Vuex from 'vuex'\nimport ThemeStore from '.\/ThemeStore'\nimport createPersistedState from 'vuex-persistedstate'\nVue.use(Vuex)\nexport default new Vuex.Store({\n  state: {},\n  mutations: {},\n  actions: {},\n  modules: {\n    Theme: ThemeStore\n  },\n  plugins: [createPersistedState()]\n})\n\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u96c6\u6210\u6210\u529f\u4e86\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528localStorage\u4f5c\u4e3a\u5b58\u50a8\u3002<\/p>\n<h3>\u4f7f\u7528Cookie\u5b58\u50a8<\/h3>\n<p>\u5b98\u65b9\u5df2\u7ecf\u63d0\u4f9b\u4f7f\u7528cookie\u5b58\u50a8\u7684\u6559\u7a0b\uff0c\u6bd4\u8f83\u65b9\u4fbf\u96c6\u6210\uff0c\u5148\u8981\u5b89\u88c5js-cookie\uff0c\u4f5c\u4e3a\u64cd\u4f5ccookie\u7684\u5de5\u5177\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a\u5176\u5b9e\u5f88\u591a\u516c\u53f8\u4e0d\u5141\u8bb8\u76f4\u63a5\u7528js\u4fee\u6539cookie\u4e86\uff0c\u8ba4\u4e3a\u662f\u4e0d\u5b89\u5168\u7684\u64cd\u4f5c\uff0ccookie\u53ea\u80fd\u540e\u53f0\u670d\u52a1\u7aef\u4fee\u6539\uff0chttpOnly\u6a21\u5f0f\uff0c\u4e2a\u4eba\u5efa\u8bae\u4e0d\u8981\u4f7f\u7528Cooke\u6765\u5b58\u50a8<\/strong><\/p>\n<pre><code class=\"language-shell line-numbers\">npm install js-cookie --save\n<\/code><\/pre>\n<p>\u96c6\u6210\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-json line-numbers\">import { Store } from \"vuex\";\nimport createPersistedState from \"vuex-persistedstate\";\nimport * as Cookies from \"js-cookie\";\nconst store = new Store({\n  \/\/ ...\n  plugins: [\n    createPersistedState({\n      storage: {\n        getItem: (key) =&gt; Cookies.get(key),\n        \/\/ Please see https:\/\/github.com\/js-cookie\/js-cookie#json, on how to handle JSON.\n        setItem: (key, value) =&gt;\n          Cookies.set(key, value, { expires: 3, secure: true }),\n        removeItem: (key) =&gt; Cookies.remove(key),\n      },\n    }),\n  ],\n});\n<\/code><\/pre>\n<h3>\u90e8\u5206\u6301\u4e45\u5316<\/h3>\n<p>\u76ee\u524d\u96c6\u6210\u540e\uff0cvuex-persistedstate\u9ed8\u8ba4\u4f1a\u628a\u6240\u6709state\u90fd\u4fdd\u5b58\u5230localStorage\u4e4b\u4e2d\uff0c\u5176\u5b9e\u5bf9\u4e8e\u5927\u90e8\u5206\u9875\u9762\u6765\u8bb2\uff0c\u9700\u8981store\u5b58\u50a8\u7684\u6570\u636e\u5f88\u591a\uff0c\u4f46\u662f\u9700\u8981\u6301\u4e45\u5316\u5230localStorage\u7684\u6570\u636e\u5e76\u4e0d\u591a\uff0c\u800c\u4e14localStorage\u5b58\u50a8\u5927\u91cf\u6570\u636e\u4e5f\u4e0d\u5408\u9002\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u90e8\u5206\u6570\u636e\u6301\u4e45\u5316\uff0c\u90e8\u5206\u6570\u636e\u53ea\u5728\u9875\u9762\u4e0a\u4f7f\u7528\u3002<\/p>\n<p>vuex-persistedstate\u63d0\u4f9b\u6709\u4e00\u4e2a<code>reducer<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5b58\u50a8Key\uff0c\u6216\u8005\u4f7f\u7528<code>paths<\/code>\u53c2\u6570\uff0c\u5efa\u8bae\u4f7f\u7528<code>paths<\/code>\u53c2\u6570\u6bd4\u8f83\u7b80\u5355<\/p>\n<pre><code class=\"language-js line-numbers\">\/\/ \u975eModule\u683c\u5f0f\uff1axxxx\n\/\/ \u4f7f\u7528\u4e86Module\u7684\u683c\u5f0f\uff1aModuleName.xxxx\uff0c\u8fd9\u91cc\u6301\u4e45\u5316\u7684\u662fTheme Module\u4e0b\u9762\u7684persistData\u6570\u636e\nconst PERSIST_PATHS = ['Theme.persistData']\nexport default new Vuex.Store({\n  state: {},\n  mutations: {},\n  actions: {},\n  modules: {\n    Theme: ThemeStore\n  },\n  plugins: [createPersistedState({\n    paths: PERSIST_PATHS\n  })]\n})\n<\/code><\/pre>\n<p>\u8fd9\u5c31\u7b97\u96c6\u6210\u5b8c\u6210\u4e86\uff0c\u9700\u8981\u6301\u4e45\u5316\u7684\u6570\u636e\u624b\u52a8\u6dfb\u52a0\u5230PERSIST_PATHS\u4e2d\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>GitHub\u5730\u5740\uff1ahttps:\/\/github.com\/fugary\/simple-element-ui-template<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vue\u9879\u76ee\u4e2d\u4f7f\u7528Vuex\u4f5c\u4e3a\u72b6\u6001\u7ba1\u7406\u5df2\u7ecf\u662f\u6bd4\u8f83\u901a\u7528\u7684\u505a\u6cd5\u4e86\uff0c\u65b0\u5efaVue\u9879\u76ee\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u9009\u62e9\u96c6\u6210Vuex\uff0c\u5176\u5b9eV [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[10],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/49"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=49"}],"version-history":[{"count":0,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/49\/revisions"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}