{"id":416,"date":"2022-12-16T14:46:44","date_gmt":"2022-12-16T06:46:44","guid":{"rendered":"https:\/\/fugary.com\/?p=416"},"modified":"2022-12-16T14:46:44","modified_gmt":"2022-12-16T06:46:44","slug":"vuex%e9%87%8d%e7%bd%aestore%e7%9a%84%e6%96%b9%e6%b3%95%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=416","title":{"rendered":"Vuex\u91cd\u7f6estore\u7684\u65b9\u6cd5\u8bb0\u5f55"},"content":{"rendered":"<p><code>Vue<\/code>\u9879\u76ee\u4e2d\u4f7f\u7528<code>Vuex<\/code>\u4f5c\u4e3a\u72b6\u6001\u7ba1\u7406\u5df2\u7ecf\u662f\u6bd4\u8f83\u901a\u7528\u7684\u505a\u6cd5\u4e86\uff0c<code>Vuex<\/code>\u672c\u8d28\u4e0a\u7c7b\u4f3c\u5168\u5c40\u7684\u53d8\u91cf\u5b58\u50a8\uff0c\u65b9\u4fbf\u5728\u6240\u6709<code>Vue<\/code>\u7ec4\u4ef6\u4e2d\u5171\u4eab\uff0c\u800c\u4e14\u4e5f\u53ef\u4ee5\u52a8\u6001\u6539\u53d8\u72b6\u6001\u3002<\/p>\n<p>\u76ee\u524d\u4f7f\u7528<code>Vuex<\/code>\u53ef\u4ee5\u628a\u767b\u5f55\u6570\u636e\u4fdd\u5b58\u5230<code>localStorage<\/code>\u4e2d\uff0c\u5b9e\u73b0\u77ed\u65f6\u95f4\u5185\u514d\u767b\u9646\u3002<\/p>\n<p>\u5b58\u50a8<code>localStorage<\/code>\u53c2\u8003\uff1a<a href=\"https:\/\/fugary.com\/?p=49\">Vue\u9879\u76ee\u96c6\u6210vuex-persistedstate<\/a><\/p>\n<p>\u4f46\u662f<code>Vuex<\/code>\u4f3c\u4e4e\u6ca1\u6709\u8003\u8651\u63d0\u4f9b\u4e00\u4e2a\u91cd\u7f6e\u6216\u8005\u6e05\u7a7a<code>state<\/code>\u7684\u65b9\u6cd5\uff0c\u9700\u8981\u81ea\u5df1\u6765\u5b9e\u73b0\u3002<\/p>\n<h3>\u6e05\u7406\u767b\u5f55\u6570\u636e<\/h3>\n<p>\u200b   \u6ce8\u9500\u65f6\u9700\u8981\u6e05\u7406<code>store<\/code>\u6bd4\u8f83\u9ebb\u70e6\uff0c<code>store<\/code>\u662f\u5728\u5185\u5b58\u4e2d\uff0c\u54ea\u6015\u662f\u5220\u9664<code>localStorage<\/code>\uff0c\u4e5f\u53ef\u80fd\u518d\u6b21\u628a\u5185\u5b58\u4e2d\u7684<code>store<\/code>\u6570\u636e\u5199\u5165<code>localStorage<\/code>\uff0c\u9020\u6210\u6ce8\u9500\u5931\u8d25\u3002<\/p>\n<h4>\u76f4\u63a5\u5220\u9664localStorage<\/h4>\n<p>\u53ef\u4ee5\u76f4\u63a5\u5220\u9664<code>localStorage<\/code>\uff0c\u5220\u9664\u540e\u8fd8\u9700\u8981\u5237\u65b0\u9875\u9762\uff0c\u4f3c\u4e4e\u4f53\u9a8c\u4e0d\u662f\u5f88\u597d\uff0c\u6574\u4e2a\u9875\u9762\u4f1a\u5237\u65b0\u3002<\/p>\n<pre><code class=\"language-js\">window.localStorage.removeItem(STORAGE_KEY)\nwindow.location.reload()<\/code><\/pre>\n<h4>\u91cd\u7f6estore<\/h4>\n<p>\u8fd8\u662f\u8003\u8651\u4ece<code>store<\/code>\u4e0b\u624b\uff0c\u91cd\u7f6e<code>store<\/code>\u7684\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5728<code>store<\/code>\u521d\u59cb\u5316\u7684\u65f6\u5019\u628a<code>store<\/code>\u4e2d\u7684\u521d\u59cb<code>state<\/code>\u5b58\u4e0b\u6765<\/li>\n<li>\u7ed9<code>actions<\/code>\u548c<code>mutations<\/code>\u589e\u52a0\u4e00\u4e2a\u91cd\u7f6e\uff08<code>__resetStoreState<\/code>\uff09\u65b9\u6cd5\uff0c\u5e76\u628a\u91cd\u7f6e\u65b9\u6cd5\u540d\u79f0\u4fdd\u5b58\u5230<strong>\u91cd\u7f6e\u65b9\u6cd5\u5217\u8868<\/strong>\u4e2d<\/li>\n<li>\u5982\u679c\u6709\u5b50\u6a21\u5757\uff0c\u9012\u5f52\u5904\u7406\u5b50\u6a21\u5757\uff0c\u91cd\u590d\u7b2c2\u6b65\u9aa4<\/li>\n<li>\u63d0\u4f9b\u4e00\u4e2a\u5916\u90e8\u8bbf\u95ee\u7684\u91cd\u7f6e\u65b9\u6cd5\uff0c\u5177\u4f53\u903b\u8f91\u5c31\u662f\u628a<strong>\u91cd\u7f6e\u65b9\u6cd5\u5217\u8868<\/strong>\u4e2d\u7684\u91cd\u7f6e\u65b9\u6cd5\u89e6\u53d1\u4e00\u4e0b\u3002<\/li>\n<\/ol>\n<h5>\u5177\u4f53\u5b9e\u73b0<\/h5>\n<p>\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff08<code>StoreHelper.js<\/code>\uff09\uff0c\u4f9d\u8d56<code>lodash<\/code>\u5e93\uff1a<\/p>\n<pre><code class=\"language-js\">import cloneDeep from &#039;lodash\/cloneDeep&#039;\nimport store from &#039;@\/store&#039;\nimport Vuex from &#039;vuex&#039;\nconst TO_RESET_STORE_STATE_KEYS = [] \/\/ \u9700\u8981\u6e05\u7406\u7684Key\nconst RESET_STORE_STATE_KEY = &#039;__resetStoreState&#039; \/\/ \u5185\u90e8\u6e05\u7406\u65b9\u6cd5\u540d\n\/**\n * store\u91cd\u7f6e\u521d\u59cb\u5316\uff0c\u9012\u5f52\u5904\u7406\u5b50\u6a21\u5757\uff0c\u5e76\u628a\u9700\u8981\u91cd\u7f6e\u7684key\u4fdd\u5b58\u4e0b\u6765\n *\n * @param configData store\u6570\u636e\n * @param excludeNames \u4e0d\u6e05\u7406\u7684\u6a21\u5757\u540d\uff0c\u6709\u4e9b\u6570\u636e\u53ef\u80fd\u9700\u8981\u5728\u6ce8\u9500\u65f6\u4e5f\u4e0d\u6e05\u7406\n * @param parentModuleKey \u6a21\u5757\u540d\u62fc\u63a5\uff0c\u5904\u7406\u5b50\u6a21\u5757\n *\/\nexport function $createStoreWithResetHandler (configData, excludeNames = [], parentModuleKey = &#039;&#039;) {\n  const resetStoreKey = parentModuleKey ? `${parentModuleKey}\/${RESET_STORE_STATE_KEY}` : RESET_STORE_STATE_KEY\n  TO_RESET_STORE_STATE_KEYS.push(resetStoreKey) \/\/ reset\u76f8\u5173key\u5b58\u5165\u6570\u7ec4\u4e2d\n  if (configData.modules) {\n    Object.keys(configData.modules).forEach(moduleName =&gt; {\n      if (excludeNames.indexOf(moduleName) === -1) {\n        const module = configData.modules[moduleName]\n        const newParentModuleKey = parentModuleKey ? `${parentModuleKey}\/${moduleName}` : moduleName\n        $createStoreWithResetHandler(module, excludeNames, newParentModuleKey)\n      }\n    })\n  }\n  if (configData.state) {\n    configData.mutations = configData.mutations || {}\n    configData.actions = configData.actions || {}\n    if (!configData.mutations[RESET_STORE_STATE_KEY]) {\n      const initialState = cloneDeep(configData.state)\n      configData.mutations[RESET_STORE_STATE_KEY] = state =&gt; Object.assign(state, cloneDeep(initialState))\n    }\n    if (!configData.actions[RESET_STORE_STATE_KEY]) {\n      configData.actions[RESET_STORE_STATE_KEY] = ({ commit }) =&gt; commit(RESET_STORE_STATE_KEY)\n    }\n  }\n  return configData\n}\n\/**\n * \u91cd\u7f6estore\u65b9\u6cd5\n *\n * @returns Promise\n *\/\nexport function $resetStoreState () { \/\/ \u9700\u8981\u91cd\u7f6e\u7684key\u5df2\u7ecf\u5b58\u4e0b\u6765\uff0c\u5faa\u73afkey\u89e6\u53d1\u91cd\u7f6e\n  const resetResults = TO_RESET_STORE_STATE_KEYS.map(resetKey =&gt; store.dispatch(resetKey))\n  return Promise.allSettled(resetResults)\n}\n\/**\n * \u5305\u88c5store\uff0c\u65b9\u4fbf\u4f7f\u7528\n *\n * @param configData\n * @param excludeNames\n * @returns {Store&lt;unknown&gt;}\n *\/\nexport function $wrapStore (configData, excludeNames = []) {\n  return new Vuex.Store($createStoreWithResetHandler(configData, excludeNames))\n}\n\/**\n * \u5bfc\u51fa$resetStoreState\u65b9\u6cd5\uff0c\u65b9\u4fbf\u4f7f\u7528\n * Vue.use(StoreHelper)\n *\/\nexport default {\n  install: (Vue, options = {}) =&gt; Object.assign(Vue.prototype, { $resetStoreState })\n}\n<\/code><\/pre>\n<h5>\u5305\u88c5store\u641c\u96c6\u6570\u636e<\/h5>\n<p>\u5305\u88c5<code>store<\/code>\u641c\u96c6<code>state<\/code>\u6570\u636e\uff0c\u4ee5\u53ca\u9700\u8981\u89e6\u53d1<code>reset<\/code>\u7684\u76f8\u5173\u65b9\u6cd5<\/p>\n<p><code>store.js<\/code>\u9ed8\u8ba4\u4ee3\u7801\uff08\u53bb\u6389<code>import<\/code>\uff09\uff1a<\/p>\n<pre><code class=\"language-js\">Vue.use(Vuex)\nexport default new Vuex.Store({\n  state: {},\n  mutations: {},\n  actions: {},\n  modules: {\n    Common: CommonStore,\n    Theme: ThemeStore\n  },\n  plugins: [createPersistedState()]\n})<\/code><\/pre>\n<p>\u4fee\u6539\u4ee3\u7801\u4e5f\u6bd4\u8f83\u7b80\u5355\uff0c\u628a<code>new Vuex.Store<\/code>\u6362\u6210<code>$wrapStore<\/code>\uff0c\u5982\u679c\u90e8\u5206\u6a21\u5757\u4e0d\u8981<code>reset<\/code>\uff0c\u53ef\u4ee5\u8fc7\u6ee4\u6389\uff1a<\/p>\n<pre><code class=\"language-js\">Vue.use(Vuex)\nVue.use(StoreHelper)\nexport default $wrapStore({\n  state: {},\n  mutations: {},\n  actions: {},\n  modules: {\n    Common: CommonStore,\n    Theme: ThemeStore\n  },\n  plugins: [createPersistedState()]\n}, [&#039;Common&#039;]) \/\/ Common\u6a21\u5757\u4e0d\u8981reset<\/code><\/pre>\n<h5>\u89e6\u53d1store\u91cd\u7f6e<\/h5>\n<p>\u5728\u9700\u8981\u7684\u5730\u65b9\u89e6\u53d1\u4e00\u4e0b\u91cd\u7f6e\uff0c\u6bd4\u5982\u6ce8\u9500\uff0c\u7136\u540e\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\u5373\u53ef<\/p>\n<pre><code class=\"language-js\">this.$resetStoreState().then(() =&gt; {\n    \/\/ \u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\n})<\/code><\/pre>\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\uff0cVuex\u672c\u8d28\u4e0a\u7c7b\u4f3c\u5168\u5c40\u7684\u53d8\u91cf\u5b58\u50a8\uff0c\u65b9\u4fbf\u5728\u6240\u6709Vue [&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":[],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/416"}],"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=416"}],"version-history":[{"count":1,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/416\/revisions"}],"predecessor-version":[{"id":417,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/416\/revisions\/417"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}