{"id":31206,"date":"2024-11-25T13:43:12","date_gmt":"2024-11-25T05:43:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31206\/"},"modified":"2024-11-25T13:43:12","modified_gmt":"2024-11-25T05:43:12","slug":"vuex%e6%a8%a1%e5%9d%97%e5%8c%96module%e5%ae%9e%e4%be%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31206\/","title":{"rendered":"Vuex\u6a21\u5757\u5316(module)\u5b9e\u4f8b\u8be6\u89e3"},"content":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u548c\u5927\u5bb6\u4ecb\u7ecdvuex \u6a21\u5757\u5316(module)\uff0c\u5c0f\u7f16\u89c9\u5f97\u633a\u4e0d\u9519\u7684\uff0c\u73b0\u5728\u5206\u4eab\u7ed9\u5927\u5bb6\uff0c\u4e5f\u7ed9\u5927\u5bb6\u505a\u4e2a\u53c2\u8003\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\u3002<\/p>\n<p>\u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u6a21\u5757\u5316<\/p>\n<p>\u524d\u9762\u6211\u4eec\u8bb2\u5230\u7684\u4f8b\u5b50\u90fd\u5728\u4e00\u4e2a\u72b6\u6001\u6811\u91cc\u8fdb\u884c\uff0c\u5f53\u4e00\u4e2a\u9879\u76ee\u6bd4\u8f83\u5927\u65f6\uff0c\u6240\u6709\u7684\u72b6\u6001\u90fd\u96c6\u4e2d\u5728\u4e00\u8d77\u4f1a\u5f97\u5230\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u5bf9\u8c61\uff0c\u8fdb\u800c\u663e\u5f97\u81c3\u80bf\uff0c\u96be\u4ee5\u7ef4\u62a4\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cVuex\u5141\u8bb8\u6211\u4eec\u5c06store\u5206\u5272\u6210\u6a21\u5757\uff08module\uff09\uff0c\u6bcf\u4e2amodule\u6709\u81ea\u5df1\u7684state\uff0cmutation\uff0caction\uff0cgetter\uff0c\u751a\u81f3\u8fd8\u53ef\u4ee5\u5f80\u4e0b\u5d4c\u5957\u6a21\u5757\uff0c\u4e0b\u9762\u6211\u4eec\u770b\u4e00\u4e2a\u5178\u578b\u7684\u6a21\u5757\u5316\u4f8b\u5b50<\/p>\n<pre>const moduleA = {\r\n state: {....},\r\n mutations: {....},\r\n actions: {....},\r\n getters: {....}\r\n}\r\n\r\nconst moduleB = {\r\n state: {....},\r\n mutations: {....},\r\n actions: {....},\r\n getters: {....}\r\n}\r\n\r\nconst store = new Vuex.Store({\r\n modules: {\r\n a: moduleA,\r\n b: moduleB\r\n }\r\n})\r\n\r\nstore.state.a \/\/ moduleA\u7684\u72b6\u6001\r\nstore.state.b \/\/ moduleB\u7684\u72b6\u6001<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e8c\u3001\u6a21\u5757\u7684\u5c40\u90e8\u72b6\u6001<\/p>\n<p>\u6a21\u5757\u5185\u90e8\u7684mutation\u548cgetter\uff0c\u63a5\u6536\u7684\u7b2c\u4e00\u53c2\u6570\uff08state\uff09\u662f\u6a21\u5757\u7684\u5c40\u90e8\u72b6\u6001\u5bf9\u8c61,rootState<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>const moduleA = {\r\n state: { count: 0},\r\n mutations: {\r\n increment (state) {\r\n  \/\/ state\u662f\u6a21\u5757\u7684\u5c40\u90e8\u72b6\u6001\uff0c\u4e5f\u5c31\u662f\u4e0a\u9762\u7684state\r\n  state.count++\r\n }\r\n },\r\n getters: {\r\n doubleCount (state, getters, rootState) {\r\n  \/\/ \u53c2\u6570 state\u4e3a\u5f53\u524d\u5c40\u90e8\u72b6\u6001\uff0crootState\u4e3a\u6839\u8282\u70b9\u72b6\u6001\r\n  return state.count * 2\r\n }\r\n },\r\n actions: {\r\n incremtnIfOddRootSum ( { state, commit, rootState } ) {\r\n  \/\/ \u53c2\u6570 state\u4e3a\u5f53\u524d\u5c40\u90e8\u72b6\u6001\uff0crootState\u4e3a\u6839\u8282\u70b9\u72b6\u6001\r\n  if ((state.cont + rootState.count) % 2 === 1) {\r\n  commit('increment')\r\n  }\r\n }\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001\u547d\u540d\u7a7a\u95f4\uff08\u8fd9\u91cc\u4e00\u5b9a\u8981\u770b\uff0c\u4e0d\u7136\u6709\u4e9b\u65f6\u5019\u4f1a\u88ab\u5751\uff09<\/p>\n<p>\u4e0a\u9762\u6240\u6709\u7684\u4f8b\u5b50\u4e2d\uff0c\u6a21\u5757\u5185\u90e8\u7684action\u3001mutation\u3001getter\u662f\u6ce8\u518c\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u7684\uff0c\u5982\u679c\u4f60\u5728moduleA\u548cmoduleB\u91cc\u5206\u522b\u58f0\u660e\u4e86\u547d\u540d\u76f8\u540c\u7684action\u6216\u8005mutation\u6216\u8005getter\uff08\u53ebsome\uff09\uff0c\u5f53\u4f60\u4f7f\u7528store.commit(&#8216;some&#8217;)\uff0cA\u548cB\u6a21\u5757\u4f1a\u540c\u65f6\u54cd\u5e94\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4f60\u7684\u6a21\u5757\u66f4\u52a0\u81ea\u5305\u542b\u548c\u63d0\u9ad8\u53ef\u91cd\u7528\u6027\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0namespaced: true\u7684\u65b9\u5f0f\uff0c\u4f7f\u5176\u6210\u4e3a\u547d\u540d\u7a7a\u95f4\u6a21\u5757\u3002\u5f53\u6a21\u5757\u88ab\u6ce8\u518c\u540e\uff0c\u5b83\u7684\u6240\u6709getter\uff0caction\uff0cmutation\u90fd\u4f1a\u81ea\u52a8\u6839\u636e\u6a21\u5757\u6ce8\u518c\u7684\u8def\u5f84\u8c03\u7528\u6574\u4e2a\u547d\u540d\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>const store = new Vuex.Store({\r\n modules: {\r\n account: {\r\n  namespaced: true,\r\n  state: {...}, \/\/ \u6a21\u5757\u5185\u7684\u72b6\u6001\u5df2\u7ecf\u662f\u5d4c\u5957\u7684\uff0cnamespaced\u4e0d\u4f1a\u6709\u5f71\u54cd\r\n  getters: {  \/\/ \u6bcf\u4e00\u6761\u6ce8\u91ca\u4e3a\u8c03\u7528\u65b9\u6cd5\r\n  isAdmin () { ... } \/\/ getters['account\/isAdmin']\r\n  },\r\n  actions: {\r\n  login () {...} \/\/ dispatch('account\/login')\r\n  },\r\n  mutations: {\r\n  login () {...} \/\/ commit('account\/login')\r\n  },\r\n  modules: {  \/\/ \u7ee7\u627f\u7236\u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4\r\n  myPage : {\r\n   state: {...},\r\n   getters: {\r\n   profile () {...}  \/\/ getters['account\/profile']\r\n   }\r\n  },\r\n  posts: { \/\/ \u8fdb\u4e00\u6b65\u5d4c\u5957\u547d\u540d\u7a7a\u95f4\r\n   namespaced: true,\r\n   getters: {\r\n   popular () {...} \/\/ getters['account\/posts\/popular']\r\n   }\r\n  }\r\n  }\r\n }\r\n }\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u542f\u7528\u4e86\u547d\u540d\u7a7a\u95f4\u7684getter\u548caction\u4f1a\u6536\u5230\u5c40\u90e8\u5316\u7684getter\uff0cdispatch\u548ccommit\u3002\u4f60\u5728\u4f7f\u7528\u6a21\u5757\u5185\u5bb9\u65f6\u4e0d\u9700\u8981\u518d\u540c\u4e00\u6a21\u5757\u5185\u6dfb\u52a0\u7a7a\u95f4\u540d\u524d\u7f00\uff0c\u66f4\u6539namespaced\u5c5e\u6027\u540e\u4e0d\u9700\u8981\u4fee\u6539\u6a21\u5757\u5185\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u56db\u3001\u5728\u547d\u540d\u7a7a\u95f4\u6a21\u5757\u5185\u8bbf\u95ee\u5168\u5c40\u5185\u5bb9\uff08Global Assets\uff09<\/p>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528\u5168\u5c40state\u548cgetter\uff0croorState\u548crootGetter\u4f1a\u4f5c\u4e3a\u7b2c\u4e09\u548c\u7b2c\u56db\u53c2\u6570\u4f20\u5165getter\uff0c\u4e5f\u4f1a\u901a\u8fc7context\u5bf9\u8c61\u7684\u5c5e\u6027\u4f20\u5165action\u82e5\u9700\u8981\u5728\u5168\u5c40\u547d\u540d\u7a7a\u95f4\u5185\u5206\u53d1action\u6216\u8005\u63d0\u4ea4mutation\uff0c\u5c06{ root: true }\u4f5c\u4e3a\u7b2c\u4e09\u53c2\u6570\u4f20\u7ed9dispatch\u6216commit\u5373\u53ef\u3002<\/p>\n<pre>modules: {\r\n foo: {\r\n namespaced: true,\r\n getters: {\r\n  \/\/ \u5728\u8fd9\u4e2a\u88ab\u547d\u540d\u7684\u6a21\u5757\u91cc\uff0cgetters\u88ab\u5c40\u90e8\u5316\u4e86\r\n  \/\/ \u4f60\u53ef\u4ee5\u4f7f\u7528getter\u7684\u7b2c\u56db\u4e2a\u53c2\u6570\u6765\u8c03\u7528 'rootGetters'\r\n  someGetter (state, getters, rootSate, rootGetters) {\r\n  getters.someOtherGetter \/\/ -&gt; \u5c40\u90e8\u7684getter\uff0c \u2018foo\/someOtherGetter'\r\n  rootGetters.someOtherGetter \/\/ -&gt; \u5168\u5c40getter, 'someOtherGetter'\r\n  }\r\n },\r\n actions: {\r\n  \/\/ \u5728\u8fd9\u4e2a\u6a21\u5757\u91cc\uff0cdispatch\u548ccommit\u4e5f\u88ab\u5c40\u90e8\u5316\u4e86\r\n  \/\/ \u4ed6\u4eec\u53ef\u4ee5\u63a5\u53d7root\u5c5e\u6027\u4ee5\u8bbf\u95ee\u8ddfdispatch\u548ccommit\r\n  smoeActino ({dispatch, commit, getters, rootGetters }) {\r\n  getters.someGetter \/\/ 'foo\/someGetter'\r\n  rootGetters.someGetter \/\/ 'someGetter'\r\n  dispatch('someOtherAction')  \/\/ 'foo\/someOtherAction'\r\n  dispatch('someOtherAction', null, {root: true}) \/\/ =&gt; \u2018someOtherAction'\r\n  commit('someMutation') \/\/ 'foo\/someMutation'\r\n  commit('someMutation', null, { root: true }) \/\/ someMutation\r\n  }\r\n }\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e94\u3001\u5e26\u547d\u540d\u7a7a\u95f4\u7684\u7ed1\u5b9a\u51fd\u6570<\/p>\n<p>\u524d\u9762\u8bf4\u8fc7\uff0c\u5e26\u4e86\u547d\u540d\u7a7a\u95f4\u540e\uff0c\u8c03\u7528\u65f6\u5fc5\u987b\u8981\u5199\u4e0a\u547d\u540d\u7a7a\u95f4\uff0c\u4f46\u662f\u8fd9\u6837\u5c31\u6bd4\u8f83\u7e41\u7410\uff0c\u5c24\u5176\u6d89\u53ca\u5230\u591a\u5c42\u5d4c\u5957\u65f6\uff08\u5f53\u7136\u5f00\u53d1\u4e2d\u522b\u5d4c\u5957\u592a\u591a\uff0c\u4f1a\u6655\u3002\u3002\uff09<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u770b\u4e0b\u4e00\u822c\u5199\u6cd5<\/p>\n<pre>computed: {\r\n ...mapState({\r\n a: state =&gt; state.some.nested.module.a,\r\n b: state =&gt; state.some.nested.module.b\r\n }),\r\n methods: {\r\n ...mapActions([\r\n  'some\/nested\/module\/foo',\r\n  'some\/nested\/module\/bar'\r\n ])\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u4f60\u53ef\u4ee5\u5c06\u6a21\u5757\u7684\u547d\u540d\u7a7a\u95f4\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f20\u9012\u7ed9\u4e0a\u8ff0\u51fd\u6570\uff0c\u8fd9\u6837\u6240\u6709\u7684\u7ed1\u5b9a\u4f1a\u81ea\u52a8\u5c06\u8be5\u6a21\u5757\u4f5c\u4e3a\u4e0a\u4e0b\u6587\u3002\u7b80\u5316\u5199\u5c31\u662f<\/p>\n<pre>computed: {\r\n ...mapStates('some\/nested\/module', {\r\n a: state =&gt; state.a,\r\n b: state =&gt; state.b\r\n })\r\n},\r\nmethods: {\r\n ...mapActions('some\/nested\/module',[\r\n 'foo',\r\n 'bar'\r\n ])\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u516d\u3001\u6a21\u5757\u91cd\u7528<\/p>\n<p>\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u521b\u5efa\u4e00\u4e2a\u6a21\u5757\u7684\u591a\u4e2a\u5b9e\u4f8b\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\n<p>\u521b\u5efa\u591a\u4e2astore\uff0c\u4ed6\u4eec\u5171\u7528\u4e00\u4e2a\u6a21\u5757<\/p>\n<\/li>\n<li>\n<p>\u5728\u4e00\u4e2astore\u4e2d\u591a\u6b21\u6ce8\u518c\u540c\u4e00\u4e2a\u6a21\u5757<\/p>\n<\/li>\n<\/ul>\n<p>\u5982\u679c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u7eaf\u5bf9\u8c61\u6765\u58f0\u660e\u6a21\u5757\u7684\u72b6\u6001\uff0c\u90a3\u4e48\u8fd9\u4e2a\u72b6\u6001\u5bf9\u8c61\u4f1a\u901a\u8fc7\u5f15\u7528\u88ab\u5171\u4eab\uff0c\u5bfc\u81f4\u6570\u636e\u4e92\u76f8\u6c61\u67d3\u3002<br \/>\u5b9e\u9645\u4e0aVue\u7ec4\u4ef6\u5185data\u662f\u540c\u6837\u7684\u95ee\u9898\uff0c\u56e0\u6b64\u89e3\u51b3\u529e\u6cd5\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u4f7f\u7528\u4e00\u4e2a\u51fd\u6570\u6765\u58f0\u660e\u6a21\u5757\u72b6\u6001\uff082.3.0+\u652f\u6301\uff09<\/p>\n<pre>const MyModule = {\r\n state () {\r\n return {\r\n  foo: 'far'\r\n }\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e03\u3001\u603b\u7ed3<\/p>\n<p>\u5230\u8fd9\u91cc\u6a21\u5757\u5316\uff08module\uff09\u7684\u5185\u5bb9\u5c31\u5df2\u7ecf\u8bb2\u5b8c\u4e86\uff0c\u672c\u6b21\u4e3b\u8981\u8bb2\u89e3\u4e86module\u51fa\u73b0\u7684\u539f\u56e0\uff0c\u4f7f\u7528\u65b9\u6cd5\uff0c\u5168\u5c40\u548c\u5c40\u90e8namespaced\u6a21\u5757\u547d\u540d\u7a7a\u95f4\uff0c\u5c40\u90e8\u8bbf\u95ee\u5168\u5c40\u5185\u5bb9\uff0cmap\u51fd\u6570\u5e26\u6709\u547d\u540d\u7a7a\u95f4\u7684\u7ed1\u5b9a\u51fd\u6570\u548c\u6a21\u5757\u7684\u91cd\u7528\u3002<\/p>\n<p>\u5f15\u7528<\/p>\n<p>https:\/\/vuex.vuejs.org Vuex\u5b98\u65b9\u6587\u6863<\/p>\n<p>\u76f8\u5173\u63a8\u8350\uff1a<\/p>\n<\/p>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fVuex\u6a21\u5757\u5316(module)\u5b9e\u4f8b\u8be6\u89e3\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u548c\u5927\u5bb6\u4ecb\u7ecdvuex \u6a21\u5757\u5316(module)\uff0c\u5c0f\u7f16\u89c9\u5f97\u633a\u4e0d\u9519\u7684\uff0c\u73b0\u5728\u5206\u4eab\u7ed9\u5927\u5bb6\uff0c\u4e5f\u7ed9\u5927\u5bb6\u505a\u4e2a\u53c2\u8003\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\u3002 \u4e00\u3001\u4e3a\u4ec0\u4e48\u9700\u8981\u6a21\u5757\u5316 \u524d\u9762\u6211\u4eec\u8bb2\u5230\u7684\u4f8b\u5b50\u90fd\u5728\u4e00\u4e2a\u72b6\u6001\u6811\u91cc\u8fdb\u884c\uff0c\u5f53\u4e00\u4e2a\u9879\u76ee\u6bd4\u8f83\u5927\u65f6\uff0c\u6240\u6709\u7684\u72b6\u6001\u90fd\u96c6\u4e2d\u5728\u4e00\u8d77\u4f1a\u5f97\u5230\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u5bf9\u8c61\uff0c\u8fdb\u800c\u663e\u5f97\u81c3\u80bf\uff0c\u96be\u4ee5\u7ef4\u62a4\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cVuex\u5141\u8bb8\u6211\u4eec\u5c06store\u5206\u5272\u6210\u6a21\u5757\uff08module\uff09\uff0c\u6bcf\u4e2amodule\u6709\u81ea\u5df1\u7684state\uff0cmutation\uff0caction\uff0cgetter\uff0c\u751a\u81f3\u8fd8\u53ef\u4ee5\u5f80\u4e0b\u5d4c\u5957\u6a21\u5757\uff0c\u4e0b\u9762\u6211\u4eec\u770b\u4e00\u4e2a\u5178\u578b\u7684\u6a21\u5757\u5316\u4f8b\u5b50 const moduleA = { state: {&#8230;.}, mutations: {&#8230;.}, actions: {&#8230;.}, getters: {&#8230;.} } const moduleB = { state: {&#8230;.}, mutations: {&#8230;.}, actions: {&#8230;.}, getters: {&#8230;.} } const store = new Vuex.Store({ modules: { a: moduleA, b: moduleB } }) store.state.a \/\/ moduleA\u7684\u72b6\u6001 store.state.b \/\/ moduleB\u7684\u72b6\u6001 \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u6a21\u5757\u7684\u5c40\u90e8\u72b6\u6001 \u6a21\u5757\u5185\u90e8\u7684mutation\u548cgetter\uff0c\u63a5\u6536\u7684\u7b2c\u4e00\u53c2\u6570\uff08state\uff09\u662f\u6a21\u5757\u7684\u5c40\u90e8\u72b6\u6001\u5bf9\u8c61,rootState \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b const moduleA [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31206","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31206"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31206\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}