{"id":52885,"date":"2025-08-11T20:54:13","date_gmt":"2025-08-11T12:54:13","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/52885.html"},"modified":"2025-08-11T20:54:13","modified_gmt":"2025-08-11T12:54:13","slug":"3-6-%e4%bf%ae%e6%94%b9vuex%e7%9a%84%e7%8a%b6%e6%80%81mutations-actions","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/52885.html","title":{"rendered":"3.6 \u4fee\u6539vuex\u7684\u72b6\u6001Mutations ,Actions"},"content":{"rendered":"<h3 id=\"%C2%A0%E2%80%8B%E7%BC%96%E8%BE%91\"><\/h3>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%C2%A0%E2%80%8B%E7%BC%96%E8%BE%91-toc\" style=\"margin-left:40px\">\u00a0\u200b\u7f16\u8f91<\/p>\n<p id=\"-toc\" style=\"margin-left:40px\">\n<p id=\"%E4%B8%8D%E8%83%BD%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9state-toc\" style=\"margin-left:40px\">\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539state<\/p>\n<p id=\"%E4%B8%8D%E5%85%81%E8%AE%B8%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9%E7%9A%84%E5%8E%9F%E5%9B%A0-toc\" style=\"margin-left:80px\">\u4e0d\u5141\u8bb8\u76f4\u63a5\u4fee\u6539\u7684\u539f\u56e0<\/p>\n<p id=\"%E5%A6%82%E4%BD%95%E5%90%AF%E7%94%A8%E4%B8%A5%E6%A0%BC%E6%A8%A1%E5%BC%8F-toc\" style=\"margin-left:80px\">\u5982\u4f55\u542f\u7528\u4e25\u683c\u6a21\u5f0f<\/p>\n<p id=\"%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9-toc\" style=\"margin-left:80px\">\u6ce8\u610f\u4e8b\u9879<\/p>\n<p id=\"%E5%A6%82%E4%BD%95%E4%BF%AE%E6%94%B9%C2%A0state-toc\" style=\"margin-left:40px\">\u5982\u4f55\u4fee\u6539\u00a0state<\/p>\n<p id=\"Mutations-toc\" style=\"margin-left:80px\">Mutations<\/p>\n<p id=\"%E5%AE%9A%E4%B9%89%20Mutations-toc\" style=\"margin-left:120px\">\u5b9a\u4e49 Mutations<\/p>\n<p id=\"%E6%8F%90%E4%BA%A4%20Mutations-toc\" style=\"margin-left:120px\">\u63d0\u4ea4 Mutations<\/p>\n<p id=\"%E4%BD%BF%E7%94%A8%20Vuex%20%E5%9C%A8%E7%BB%84%E4%BB%B6%E5%86%85%E6%8F%90%E4%BA%A4%20Mutations-toc\" style=\"margin-left:120px\">\u4f7f\u7528 Vuex \u5728\u7ec4\u4ef6\u5185\u63d0\u4ea4 Mutations<\/p>\n<p id=\"actions-toc\" style=\"margin-left:80px\">actions<\/p>\n<p id=\"%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%20Actions%EF%BC%9F-toc\" style=\"margin-left:120px\">\u4e3a\u4ec0\u4e48\u9700\u8981 Actions&#xff1f;<\/p>\n<p id=\"%E5%AE%9A%E4%B9%89%20Actions-toc\" style=\"margin-left:120px\">\u5b9a\u4e49 Actions<\/p>\n<p id=\"%E5%88%86%E5%8F%91%20Actions-toc\" style=\"margin-left:120px\">\u5206\u53d1 Actions<\/p>\n<p id=\"%E5%9C%A8%E7%BB%84%E4%BB%B6%E4%B8%AD%E4%BD%BF%E7%94%A8%20Actions-toc\" style=\"margin-left:120px\">\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528 Actions<\/p>\n<p id=\"Actions%20%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC-toc\" style=\"margin-left:120px\">Actions \u7684\u8fd4\u56de\u503c<\/p>\n<p id=\"%E6%80%BB%E7%BB%93-toc\" style=\"margin-left:120px\">\u603b\u7ed3<\/p>\n<hr id=\"hr-toc\" \/>\n<h3><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"820\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811125412-6899e7f41dd15.png\" width=\"1198\" \/><\/h3>\n<h3 id=\"\"><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"332\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811125412-6899e7f486b4c.png\" width=\"507\" \/><\/p>\n<h3 id=\"%E4%B8%8D%E8%83%BD%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9state\">\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539state<\/h3>\n<p>state \u5bf9\u8c61\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539\u3002\u8fd9\u662f\u56e0\u4e3a Vuex \u8bbe\u8ba1\u7684\u6838\u5fc3\u539f\u5219\u4e4b\u4e00\u662f\u72b6\u6001\u7684\u5355\u4e00\u6570\u636e\u6e90\u548c\u72b6\u6001\u7684\u4e0d\u53ef\u53d8\u6027&#xff08;immutability&#xff09;\u3002Vuex \u7684 state \u662f\u54cd\u5e94\u5f0f\u7684&#xff0c;\u8fd9\u610f\u5473\u7740\u5f53 state \u53d1\u751f\u53d8\u5316\u65f6&#xff0c;Vue \u80fd\u591f\u68c0\u6d4b\u5230\u8fd9\u4e9b\u53d8\u5316\u5e76\u66f4\u65b0\u89c6\u56fe\u3002\u7136\u800c&#xff0c;\u4e3a\u4e86\u786e\u4fdd\u72b6\u6001\u7684\u53d8\u5316\u662f\u53ef\u8ffd\u8e2a\u548c\u9884\u6d4b\u7684&#xff0c;Vuex \u5f3a\u5236\u6240\u6709\u7684\u72b6\u6001\u53d8\u66f4\u90fd\u5fc5\u987b\u901a\u8fc7\u660e\u786e\u7684\u63d0\u4ea4&#xff08;commit&#xff09;\u6765\u5b8c\u6210\u3002<\/p>\n<h4 id=\"%E4%B8%8D%E5%85%81%E8%AE%B8%E7%9B%B4%E6%8E%A5%E4%BF%AE%E6%94%B9%E7%9A%84%E5%8E%9F%E5%9B%A0\">\u4e0d\u5141\u8bb8\u76f4\u63a5\u4fee\u6539\u7684\u539f\u56e0<\/h4>\n<li>\n<p>\u54cd\u5e94\u5f0f\u673a\u5236&#xff1a;\u5982\u679c\u76f4\u63a5\u4fee\u6539 state&#xff0c;Vue \u7684\u54cd\u5e94\u5f0f\u7cfb\u7edf\u53ef\u80fd\u65e0\u6cd5\u68c0\u6d4b\u5230\u8fd9\u4e9b\u53d8\u5316&#xff0c;\u5bfc\u81f4\u89c6\u56fe\u4e0d\u4f1a\u6b63\u786e\u66f4\u65b0\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u8bd5\u548c\u5f00\u53d1\u5de5\u5177\u652f\u6301&#xff1a;Vuex \u63d0\u4f9b\u4e86\u65f6\u95f4\u65c5\u884c\u548c\u72b6\u6001\u5feb\u7167\u7b49\u529f\u80fd&#xff0c;\u8fd9\u4e9b\u529f\u80fd\u4f9d\u8d56\u4e8e\u5bf9\u72b6\u6001\u53d8\u66f4\u7684\u660e\u786e\u8bb0\u5f55\u3002\u5982\u679c\u53ef\u4ee5\u968f\u610f\u4fee\u6539 state&#xff0c;\u90a3\u4e48\u8c03\u8bd5\u5c06\u4f1a\u53d8\u5f97\u975e\u5e38\u56f0\u96be\u3002<\/p>\n<\/li>\n<li>\n<p>\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027&#xff1a;\u901a\u8fc7\u5b9a\u4e49\u660e\u786e\u7684 mutation \u6765\u6539\u53d8\u72b6\u6001&#xff0c;\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u3001\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<\/li>\n<p>\u4e3a\u4e86\u8ba9\u76f4\u63a5\u4fee\u6539 Vuex \u7684 state \u62a5\u9519&#xff0c;\u4f60\u53ef\u4ee5\u5229\u7528 Vue \u548c Vuex \u63d0\u4f9b\u7684\u4e25\u683c\u6a21\u5f0f&#xff08;strict mode&#xff09;\u3002\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b&#xff0c;\u4efb\u4f55\u4e0d\u5728 mutation \u51fd\u6570\u4e2d\u8fdb\u884c\u7684 state \u4fee\u6539\u90fd\u4f1a\u629b\u51fa\u9519\u8bef\u3002\u8fd9\u6709\u52a9\u4e8e\u786e\u4fdd\u6240\u6709\u7684\u72b6\u6001\u53d8\u66f4\u90fd\u662f\u901a\u8fc7\u660e\u786e\u7684\u63d0\u4ea4\u6765\u5b8c\u6210\u7684&#xff0c;\u4ece\u800c\u589e\u5f3a\u5e94\u7528\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u8c03\u8bd5\u80fd\u529b\u3002<\/p>\n<h4 id=\"%E5%A6%82%E4%BD%95%E5%90%AF%E7%94%A8%E4%B8%A5%E6%A0%BC%E6%A8%A1%E5%BC%8F\">\u5982\u4f55\u542f\u7528\u4e25\u683c\u6a21\u5f0f<\/h4>\n<p>\u5728\u521b\u5efa Vuex store \u65f6&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e strict: true \u6765\u542f\u7528\u4e25\u683c\u6a21\u5f0f&#xff1a;<\/p>\n<p>const store &#061; new Vuex.Store({<br \/>\n  state: {<br \/>\n    \/\/ your state<br \/>\n  },<br \/>\n  mutations: {<br \/>\n    \/\/ your mutations<br \/>\n  },<br \/>\n  strict: true \/\/ \u542f\u7528\u4e25\u683c\u6a21\u5f0f<br \/>\n}) <\/p>\n<h4 id=\"%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\">\u6ce8\u610f\u4e8b\u9879<\/h4>\n<li>\n<p>\u5f00\u53d1\u73af\u5883&#xff1a;\u4e25\u683c\u6a21\u5f0f\u4f1a\u5bf9\u6027\u80fd\u6709\u8f7b\u5fae\u5f71\u54cd&#xff0c;\u56e0\u4e3a\u5b83\u4f1a\u5728\u6bcf\u6b21\u72b6\u6001\u53d8\u66f4\u65f6\u8fdb\u884c\u989d\u5916\u7684\u68c0\u67e5\u3002\u56e0\u6b64&#xff0c;\u5efa\u8bae\u53ea\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u542f\u7528\u4e25\u683c\u6a21\u5f0f&#xff0c;\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u5e94\u5c06\u5176\u5173\u95ed\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6027\u80fd\u5f00\u9500\u3002<\/p>\n<\/li>\n<li>\n<p>\u751f\u4ea7\u73af\u5883\u7981\u7528&#xff1a;\u901a\u5e38\u60c5\u51b5\u4e0b&#xff0c;\u4f60\u4f1a\u6839\u636e\u73af\u5883\u53d8\u91cf\u6765\u51b3\u5b9a\u662f\u5426\u542f\u7528\u4e25\u683c\u6a21\u5f0f\u3002\u4f8b\u5982&#xff0c;\u53ef\u4ee5\u8fd9\u6837\u505a&#xff1a;<\/p>\n<p> const store &#061; new Vuex.Store({<br \/>\n  state: {<br \/>\n    \/\/ your state<br \/>\n  },<br \/>\n  mutations: {<br \/>\n    \/\/ your mutations<br \/>\n  },<br \/>\n  strict: process.env.NODE_ENV !&#061;&#061; &#039;production&#039; \/\/ \u6839\u636e\u73af\u5883\u53d8\u91cf\u5224\u65ad<br \/>\n}) <\/li>\n<\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f&#xff0c;\u4f60\u53ef\u4ee5\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u6355\u83b7\u6240\u6709\u5bf9 Vuex \u72b6\u6001\u7684\u975e\u6cd5\u76f4\u63a5\u4fee\u6539&#xff0c;\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u9075\u5faa Vuex \u7684\u6700\u4f73\u5b9e\u8df5&#xff0c;\u5373\u6240\u6709\u7684\u72b6\u6001\u53d8\u66f4\u90fd\u5e94\u8be5\u901a\u8fc7 mutation \u8fdb\u884c\u3002\u8fd9\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027&#xff0c;\u4e5f\u4f7f\u5f97\u8c03\u8bd5\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<h3 id=\"%E5%A6%82%E4%BD%95%E4%BF%AE%E6%94%B9%C2%A0state\">\u5982\u4f55\u4fee\u6539\u00a0state<\/h3>\n<h4 id=\"Mutations\">Mutations<\/h4>\n<p>\u4e3a\u4e86\u4fee\u6539 Vuex \u4e2d\u7684 state&#xff0c;\u4f60\u9700\u8981\u4f7f\u7528 mutations\u3002Mutations \u662f\u552f\u4e00\u80fd\u591f\u6539\u53d8 state \u7684\u65b9\u5f0f&#xff0c;\u5e76\u4e14\u5b83\u4eec\u5fc5\u987b\u662f\u540c\u6b65\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u6240\u6709\u72b6\u6001\u7684\u53d8\u66f4\u90fd\u5fc5\u987b\u901a\u8fc7 mutations \u6765\u8fdb\u884c\u3002\u8fd9\u6837\u505a\u7684\u597d\u5904\u662f\u53ef\u4ee5\u96c6\u4e2d\u7ba1\u7406\u548c\u8ffd\u8e2a\u6240\u6709\u7684\u72b6\u6001\u53d8\u5316&#xff0c;\u6709\u52a9\u4e8e\u8c03\u8bd5\u548c\u7ef4\u62a4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50&#xff1a;<\/p>\n<p>\/\/ \u5728 store \u4e2d\u5b9a\u4e49 mutation<br \/>\nconst store &#061; new Vuex.Store({<br \/>\n  state: {<br \/>\n    count: 0<br \/>\n  },<br \/>\n  mutations: {<br \/>\n    increment(state) {<br \/>\n      \/\/ \u53d8\u66f4\u72b6\u6001<br \/>\n      state.count&#043;&#043;<br \/>\n    }<br \/>\n  }<br \/>\n})<\/p>\n<p>\/\/ \u5728\u7ec4\u4ef6\u4e2d\u63d0\u4ea4 mutation<br \/>\nthis.$store.commit(&#039;increment&#039;) <\/p>\n<p>\u57fa\u672c\u6982\u5ff5<\/p>\n<li>\n<p>\u540c\u6b65\u6027: Mutations \u5fc5\u987b\u662f\u540c\u6b65\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u5f53\u4f60\u8c03\u7528\u4e00\u4e2a mutation handler&#xff0c;\u8fd9\u4e2a\u8c03\u7528\u5b8c\u6210\u540e&#xff0c;\u72b6\u6001\u5e94\u8be5\u5df2\u7ecf\u88ab\u66f4\u65b0\u4e86\u3002\u8fd9\u4f7f\u5f97 Vuex \u80fd\u591f\u8bb0\u5f55\u6bcf\u4e00\u6b21\u7684\u72b6\u6001\u53d8\u66f4&#xff0c;\u5e76\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528 devtools \u8fdb\u884c\u65f6\u95f4\u65c5\u884c\u8c03\u8bd5&#xff08;time travel debugging&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>Payload: \u4f60\u53ef\u4ee5\u5411 mutations \u63d0\u4ea4\u989d\u5916\u7684\u53c2\u6570&#xff0c;\u8fd9\u4e9b\u53c2\u6570\u88ab\u79f0\u4e3a payload&#xff08;\u8f7d\u8377&#xff09;\u3002\u8fd9\u5141\u8bb8\u4f60\u4f20\u9012\u9700\u8981\u7684\u6570\u636e\u5230 mutation \u51fd\u6570\u4e2d\u3002<\/p>\n<\/li>\n<li>\n<p>\u63d0\u4ea4\u98ce\u683c: \u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u65b9\u5f0f\u63d0\u4ea4 mutations&#xff1a;\u666e\u901a\u98ce\u683c\u548c\u5bf9\u8c61\u98ce\u683c\u3002<\/p>\n<\/li>\n<h5 id=\"%E5%AE%9A%E4%B9%89%20Mutations\">\u5b9a\u4e49 Mutations<\/h5>\n<p>\u5728 Vuex store \u4e2d\u5b9a\u4e49 mutations&#xff1a;<\/p>\n<p>const store &#061; new Vuex.Store({<br \/>\n  state: {<br \/>\n    count: 0<br \/>\n  },<br \/>\n  mutations: {<br \/>\n    increment(state) {<br \/>\n      \/\/ \u4fee\u6539\u72b6\u6001<br \/>\n      state.count&#043;&#043;<br \/>\n    },<br \/>\n    decrement(state, payload) {<br \/>\n      \/\/ \u4f7f\u7528\u8f7d\u8377\u6765\u4fee\u6539\u72b6\u6001<br \/>\n      state.count -&#061; payload.amount<br \/>\n    }<br \/>\n  }<br \/>\n}) <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d&#xff0c;\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a mutations: increment \u548c decrement\u3002increment \u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570&#xff0c;\u800c decrement \u63a5\u53d7\u4e00\u4e2a\u5305\u542b amount \u5c5e\u6027\u7684\u5bf9\u8c61\u4f5c\u4e3a\u5176\u8f7d\u8377\u3002<\/p>\n<h5 id=\"%E6%8F%90%E4%BA%A4%20Mutations\">\u63d0\u4ea4 Mutations<\/h5>\n<p>\u53ef\u4ee5\u901a\u8fc7 store.commit \u65b9\u6cd5\u6765\u63d0\u4ea4 mutations\u3002\u6709\u4e24\u79cd\u63d0\u4ea4\u98ce\u683c&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u666e\u901a\u98ce\u683c<\/p>\n<p> store.commit(&#039;increment&#039;) <\/li>\n<li>\n<p>\u5bf9\u8c61\u98ce\u683c<\/p>\n<p> \u00a0 <\/p>\n<p>\u5bf9\u8c61\u98ce\u683c\u5141\u8bb8\u4f60\u5728\u63d0\u4ea4\u65f6\u540c\u65f6\u6307\u5b9a mutation \u7c7b\u578b\u548c\u8f7d\u8377\u3002<\/p>\n<p> store.commit({<br \/>\n  type: &#039;decrement&#039;,<br \/>\n  amount: 10<br \/>\n}) <\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u5e26\u8f7d\u8377\u7684 mutation \u8c03\u7528&#xff0c;\u4f60\u4e5f\u53ef\u4ee5\u8fd9\u6837\u5199&#xff1a;<\/p>\n<p>store.commit(&#039;decrement&#039;, { amount: 10 }) <\/p>\n<h5 id=\"%E4%BD%BF%E7%94%A8%20Vuex%20%E5%9C%A8%E7%BB%84%E4%BB%B6%E5%86%85%E6%8F%90%E4%BA%A4%20Mutations\">\u4f7f\u7528 Vuex \u5728\u7ec4\u4ef6\u5185\u63d0\u4ea4 Mutations<\/h5>\n<p>\u5728 Vue \u7ec4\u4ef6\u5185\u90e8&#xff0c;\u4f60\u53ef\u4ee5\u901a\u8fc7 this.$store.commit \u6765\u63d0\u4ea4 mutations&#xff0c;\u6216\u8005\u66f4\u63a8\u8350\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 mapMutations \u8f85\u52a9\u51fd\u6570\u5c06\u7ec4\u4ef6\u7684\u65b9\u6cd5\u6620\u5c04\u4e3a store.commit \u8c03\u7528\u3002<\/p>\n<p>\u4f7f\u7528 mapMutations:<\/p>\n<p>import { mapMutations } from &#039;vuex&#039;<\/p>\n<p>export default {<br \/>\n  methods: {<br \/>\n    &#8230;mapMutations([<br \/>\n      &#039;increment&#039;,<br \/>\n      &#039;decrement&#039;<br \/>\n    ]),<br \/>\n    someMethod() {<br \/>\n      this.increment()<br \/>\n      this.decrement({ amount: 5 })<br \/>\n    }<br \/>\n  }<br \/>\n} <\/p>\n<p>\u8fd9\u6837\u505a\u53ef\u4ee5\u8ba9\u7ec4\u4ef6\u7684\u65b9\u6cd5\u76f4\u63a5\u6620\u5c04\u5230\u5bf9\u5e94\u7684 mutations \u4e0a&#xff0c;\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<p>Mutations \u662f Vuex \u4e2d\u7528\u4e8e\u4fee\u6539 state \u7684\u552f\u4e00\u5408\u6cd5\u9014\u5f84&#xff0c;\u786e\u4fdd\u4e86\u6240\u6709\u72b6\u6001\u7684\u53d8\u5316\u90fd\u662f\u53ef\u9884\u6d4b\u548c\u53ef\u8ffd\u8e2a\u7684\u3002\u901a\u8fc7\u5b9a\u4e49\u660e\u786e\u7684 mutations \u5e76\u9075\u5faa\u4e25\u683c\u7684\u63d0\u4ea4\u89c4\u5219&#xff0c;\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7ba1\u7406\u5e94\u7528\u7684\u72b6\u6001&#xff0c;\u63d0\u5347\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<h4 id=\"actions\">actions<\/h4>\n<p>\u5982\u679c\u4f60\u9700\u8981\u6267\u884c\u5f02\u6b65\u64cd\u4f5c&#xff0c;\u5e94\u8be5\u4f7f\u7528 actions\u3002Actions \u53ef\u4ee5\u5305\u542b\u4efb\u610f\u5f02\u6b65\u64cd\u4f5c&#xff0c;\u5e76\u4e14\u6700\u7ec8\u901a\u8fc7\u63d0\u4ea4 mutation \u6765\u6539\u53d8\u72b6\u6001\u3002<\/p>\n<p>\/\/ \u5728 store \u4e2d\u5b9a\u4e49 action<br \/>\nconst store &#061; new Vuex.Store({<br \/>\n  actions: {<br \/>\n    incrementAsync({ commit }) {<br \/>\n      setTimeout(() &#061;&gt; {<br \/>\n        commit(&#039;increment&#039;)<br \/>\n      }, 1000)<br \/>\n    }<br \/>\n  }<br \/>\n})<\/p>\n<p>\/\/ \u5728\u7ec4\u4ef6\u4e2d\u5206\u53d1 action<br \/>\nthis.$store.dispatch(&#039;incrementAsync&#039;) <\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f&#xff0c;\u4f60\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u72b6\u6001\u7684\u66f4\u6539\u90fd\u662f\u900f\u660e\u548c\u53ef\u63a7\u7684&#xff0c;\u8fd9\u6709\u52a9\u4e8e\u4fdd\u6301\u5e94\u7528\u7684\u4e00\u81f4\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>Actions \u7c7b\u4f3c\u4e8e Mutations&#xff0c;\u4f46\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e&#xff1a;<\/p>\n<ul>\n<li>Actions \u63d0\u4ea4\u7684\u662f Mutations&#xff0c;\u800c\u4e0d\u662f\u76f4\u63a5\u53d8\u66f4\u72b6\u6001\u3002<\/li>\n<li>Actions \u53ef\u4ee5\u5305\u542b\u4efb\u610f\u5f02\u6b65\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u7b80\u5355\u6765\u8bf4&#xff0c;Actions \u662f\u5904\u7406\u4e1a\u52a1\u903b\u8f91&#xff08;\u5c24\u5176\u662f\u5f02\u6b65\u64cd\u4f5c&#xff09;\u7684\u5730\u65b9&#xff0c;\u800c Mutations \u662f\u552f\u4e00\u4fee\u6539\u72b6\u6001\u7684\u5730\u65b9\u3002<\/p>\n<hr \/>\n<h5 id=\"%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%20Actions%EF%BC%9F\">\u4e3a\u4ec0\u4e48\u9700\u8981 Actions&#xff1f;<\/h5>\n<li>\u5904\u7406\u5f02\u6b65\u64cd\u4f5c&#xff1a;Vuex \u7684\u6838\u5fc3\u539f\u5219\u662f\u00a0Mutations \u5fc5\u987b\u662f\u540c\u6b65\u51fd\u6570\u3002\u8fd9\u662f\u4e3a\u4e86\u786e\u4fdd\u72b6\u6001\u53d8\u66f4\u7684\u53ef\u9884\u6d4b\u6027&#xff0c;\u4f7f\u5f97 Vue Devtools \u53ef\u4ee5\u51c6\u786e\u5730\u8bb0\u5f55\u6bcf\u4e00\u6b21\u72b6\u6001\u53d8\u66f4\u3002\u5982\u679c\u5141\u8bb8\u5728 Mutations \u4e2d\u6267\u884c\u5f02\u6b65\u64cd\u4f5c&#xff0c;\u6211\u4eec\u5c31\u65e0\u6cd5\u786e\u5207\u77e5\u9053\u72b6\u6001\u662f\u5728\u4f55\u65f6\u3001\u4ee5\u4f55\u79cd\u987a\u5e8f\u53d1\u751f\u7684\u6539\u53d8\u3002<\/li>\n<li>\u5c01\u88c5\u590d\u6742\u903b\u8f91&#xff1a;Actions \u5141\u8bb8\u4f60\u5728\u63d0\u4ea4 Mutation \u4e4b\u524d\u6267\u884c\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91&#xff0c;\u6bd4\u5982\u6570\u636e\u9a8c\u8bc1\u3001\u7ec4\u5408\u591a\u4e2a\u5f02\u6b65\u8bf7\u6c42\u3001\u6761\u4ef6\u5224\u65ad\u7b49\u3002<\/li>\n<hr \/>\n<h5 id=\"%E5%AE%9A%E4%B9%89%20Actions\">\u5b9a\u4e49 Actions<\/h5>\n<p>\u5728 Vuex store \u7684 actions \u9009\u9879\u4e2d\u5b9a\u4e49 Actions\u3002\u6bcf\u4e2a Action \u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u540d\u4e3a context \u7684\u5bf9\u8c61\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\u8fd9\u4e2a context \u5bf9\u8c61\u5305\u542b\u4ee5\u4e0b\u5c5e\u6027\u548c\u65b9\u6cd5&#xff1a;<\/p>\n<ul>\n<li>state: \u6307\u5411\u00a0store.state\u3002<\/li>\n<li>rootState: \u6307\u5411\u6839\u6a21\u5757\u7684 state&#xff08;\u5728\u6a21\u5757\u5316 store \u4e2d\u4f7f\u7528&#xff09;\u3002<\/li>\n<li>commit: \u7528\u4e8e\u63d0\u4ea4 Mutation\u3002<\/li>\n<li>dispatch: \u7528\u4e8e\u5206\u53d1&#xff08;\u8c03\u7528&#xff09;\u5176\u4ed6 Action\u3002<\/li>\n<li>getters: \u6307\u5411\u00a0store.getters\u3002<\/li>\n<li>rootGetters: \u6307\u5411\u6839\u6a21\u5757\u7684 getters&#xff08;\u5728\u6a21\u5757\u5316 store \u4e2d\u4f7f\u7528&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u901a\u5e38&#xff0c;\u6211\u4eec\u4f7f\u7528 ES2015 \u7684\u53c2\u6570\u89e3\u6784\u8bed\u6cd5\u6765\u76f4\u63a5\u63d0\u53d6 commit&#xff08;\u4ee5\u53ca state, getters \u7b49&#xff09;&#xff0c;\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u3002<\/p>\n<p>const store &#061; new Vuex.Store({<br \/>\n  state: {<br \/>\n    count: 0,<br \/>\n    userInfo: null<br \/>\n  },<br \/>\n  mutations: {<br \/>\n    increment(state) {<br \/>\n      state.count&#043;&#043;<br \/>\n    },<br \/>\n    setUserInfo(state, userInfo) {<br \/>\n      state.userInfo &#061; userInfo<br \/>\n    }<br \/>\n  },<br \/>\n  actions: {<br \/>\n    \/\/ \u57fa\u672c\u5f62\u5f0f&#xff1a;\u4f7f\u7528 context.commit<br \/>\n    incrementAsync(context) {<br \/>\n      setTimeout(() &#061;&gt; {<br \/>\n        context.commit(&#039;increment&#039;) \/\/ \u63d0\u4ea4 mutation<br \/>\n      }, 1000)<br \/>\n    },<\/p>\n<p>    \/\/ \u4f7f\u7528\u53c2\u6570\u89e3\u6784&#xff1a;\u76f4\u63a5\u89e3\u6784\u51fa commit<br \/>\n    incrementAsync2({ commit }) {<br \/>\n      setTimeout(() &#061;&gt; {<br \/>\n        commit(&#039;increment&#039;)<br \/>\n      }, 1000)<br \/>\n    },<\/p>\n<p>    \/\/ \u5305\u542b\u5f02\u6b65\u64cd\u4f5c\u548c\u590d\u6742\u903b\u8f91<br \/>\n    fetchUserInfo({ commit, state }) {<br \/>\n      \/\/ \u53ef\u4ee5\u68c0\u67e5\u5f53\u524d\u72b6\u6001&#xff0c;\u4f8b\u5982\u907f\u514d\u91cd\u590d\u8bf7\u6c42<br \/>\n      if (state.userInfo) {<br \/>\n        console.log(&#039;User info already exists, skipping fetch.&#039;)<br \/>\n        return<br \/>\n      }<\/p>\n<p>      \/\/ \u6a21\u62df\u5f02\u6b65 API \u8c03\u7528<br \/>\n      api.getUserInfo().then(userInfo &#061;&gt; {<br \/>\n        commit(&#039;setUserInfo&#039;, userInfo) \/\/ \u6210\u529f\u540e\u63d0\u4ea4 mutation<br \/>\n      }).catch(error &#061;&gt; {<br \/>\n        console.error(&#039;Failed to fetch user info:&#039;, error)<br \/>\n        \/\/ \u53ef\u4ee5\u63d0\u4ea4\u4e00\u4e2a\u9519\u8bef\u5904\u7406\u7684 mutation<br \/>\n        \/\/ commit(&#039;setError&#039;, error.message)<br \/>\n      })<br \/>\n    },<\/p>\n<p>    \/\/ \u7ec4\u5408\u591a\u4e2a actions (\u4f7f\u7528 dispatch)<br \/>\n    initializeApp({ dispatch }) {<br \/>\n      \/\/ \u53ef\u4ee5\u5e76\u884c\u6216\u4e32\u884c\u5730\u8c03\u7528\u5176\u4ed6 actions<br \/>\n      dispatch(&#039;fetchUserInfo&#039;)<br \/>\n      dispatch(&#039;fetchSettings&#039;)<br \/>\n      \/\/ &#8230; \u5176\u4ed6\u521d\u59cb\u5316\u903b\u8f91<br \/>\n    }<br \/>\n  }<br \/>\n}) <\/p>\n<hr \/>\n<h5 id=\"%E5%88%86%E5%8F%91%20Actions\">\u5206\u53d1 Actions<\/h5>\n<p>\u4f7f\u7528 store.dispatch \u65b9\u6cd5\u6765\u89e6\u53d1&#xff08;\u5206\u53d1&#xff09;Actions\u3002\u4e0e commit \u4e0d\u540c&#xff0c;dispatch \u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a Promise&#xff08;\u5982\u679c Action \u5185\u90e8\u8fd4\u56de\u4e86 Promise&#xff09;&#xff0c;\u8fd9\u4f7f\u5f97\u4f60\u53ef\u4ee5\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u5b8c\u6210\u72b6\u6001\u3002<\/p>\n<p>\/\/ 1. \u57fa\u672c\u5206\u53d1<br \/>\nstore.dispatch(&#039;incrementAsync&#039;)<\/p>\n<p>\/\/ 2. \u5206\u53d1\u65f6\u643a\u5e26\u8f7d\u8377 (Payload)<br \/>\nstore.dispatch(&#039;fetchUserInfo&#039;, { userId: 123 }) \/\/ \u8f7d\u8377\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570<br \/>\n\/\/ \u5728 Action \u4e2d&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u7b2c\u4e8c\u4e2a\u53c2\u6570\u63a5\u6536\u8f7d\u8377<br \/>\n\/\/ actions: { fetchUserInfo({ commit }, payload) { &#8230; } }<\/p>\n<p>\/\/ 3. \u4f7f\u7528\u5bf9\u8c61\u98ce\u683c\u5206\u53d1<br \/>\nstore.dispatch({<br \/>\n  type: &#039;fetchUserInfo&#039;,<br \/>\n  userId: 123<br \/>\n})<\/p>\n<p>\/\/ 4. \u5904\u7406\u8fd4\u56de\u7684 Promise<br \/>\nstore.dispatch(&#039;fetchUserInfo&#039;).then(() &#061;&gt; {<br \/>\n  console.log(&#039;User info fetched successfully!&#039;)<br \/>\n}).catch(error &#061;&gt; {<br \/>\n  console.error(&#039;Fetch failed:&#039;, error)<br \/>\n}) <\/p>\n<hr \/>\n<h5 id=\"%E5%9C%A8%E7%BB%84%E4%BB%B6%E4%B8%AD%E4%BD%BF%E7%94%A8%20Actions\">\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528 Actions<\/h5>\n<p>\u5728 Vue \u7ec4\u4ef6\u4e2d&#xff0c;\u53ef\u4ee5\u901a\u8fc7 this.$store.dispatch \u6765\u5206\u53d1 Actions&#xff0c;\u6216\u8005\u4f7f\u7528 mapActions \u8f85\u52a9\u51fd\u6570\u3002<\/p>\n<p>\u65b9\u6cd5\u4e00&#xff1a;\u76f4\u63a5\u4f7f\u7528 this.$store.dispatch<\/p>\n<p>export default {<br \/>\n  methods: {<br \/>\n    async handleFetchUser() {<br \/>\n      try {<br \/>\n        await this.$store.dispatch(&#039;fetchUserInfo&#039;)<br \/>\n        console.log(&#039;User loaded!&#039;)<br \/>\n      } catch (error) {<br \/>\n        console.error(&#039;Error:&#039;, error)<br \/>\n      }<br \/>\n    }<br \/>\n  }<br \/>\n} <\/p>\n<p>\u65b9\u6cd5\u4e8c&#xff1a;\u4f7f\u7528 mapActions \u8f85\u52a9\u51fd\u6570<\/p>\n<p>import { mapActions } from &#039;vuex&#039;<\/p>\n<p>export default {<br \/>\n  methods: {<br \/>\n    \/\/ \u5c06 this.incrementAsync() \u6620\u5c04\u4e3a this.$store.dispatch(&#039;incrementAsync&#039;)<br \/>\n    &#8230;mapActions([<br \/>\n      &#039;incrementAsync&#039;,<br \/>\n      &#039;fetchUserInfo&#039;<br \/>\n    ]),<\/p>\n<p>    \/\/ \u4e5f\u53ef\u4ee5\u5c06 action \u6620\u5c04\u5230\u4e0d\u540c\u7684\u65b9\u6cd5\u540d<br \/>\n    &#8230;mapActions({<br \/>\n      loadUser: &#039;fetchUserInfo&#039; \/\/ \u5c06 this.loadUser() \u6620\u5c04\u4e3a this.$store.dispatch(&#039;fetchUserInfo&#039;)<br \/>\n    }),<\/p>\n<p>    async handleInitialize() {<br \/>\n      \/\/ \u4f7f\u7528\u6620\u5c04\u540e\u7684\u65b9\u6cd5<br \/>\n      await this.loadUser()<br \/>\n      \/\/ \u6216\u8005<br \/>\n      \/\/ await this.fetchUserInfo()<br \/>\n    }<br \/>\n  }<br \/>\n} <\/p>\n<hr \/>\n<h5 id=\"Actions%20%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC\">Actions \u7684\u8fd4\u56de\u503c<\/h5>\n<p>Actions \u53ef\u4ee5\u8fd4\u56de\u4efb\u4f55\u503c&#xff0c;\u4f46\u901a\u5e38\u8fd4\u56de\u4e00\u4e2a Promise&#xff0c;\u4ee5\u4fbf\u8c03\u7528\u8005\u53ef\u4ee5\u77e5\u9053\u5f02\u6b65\u64cd\u4f5c\u4f55\u65f6\u5b8c\u6210\u3002<\/p>\n<p>actions: {<br \/>\n  async fetchUserInfoAsync({ commit }) {<br \/>\n    try {<br \/>\n      const userInfo &#061; await api.getUserInfo() \/\/ \u5047\u8bbe api.getUserInfo \u8fd4\u56de Promise<br \/>\n      commit(&#039;setUserInfo&#039;, userInfo)<br \/>\n      return userInfo \/\/ \u8fd4\u56de\u83b7\u53d6\u5230\u7684\u6570\u636e<br \/>\n    } catch (error) {<br \/>\n      console.error(error)<br \/>\n      throw error \/\/ \u91cd\u65b0\u629b\u51fa\u9519\u8bef&#xff0c;\u8ba9\u8c03\u7528\u8005\u5904\u7406<br \/>\n    }<br \/>\n  }<br \/>\n}<\/p>\n<p>\/\/ \u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528<br \/>\nmethods: {<br \/>\n  async loadUser() {<br \/>\n    try {<br \/>\n      const userInfo &#061; await this.$store.dispatch(&#039;fetchUserInfoAsync&#039;)<br \/>\n      console.log(&#039;Fetched user:&#039;, userInfo)<br \/>\n    } catch (error) {<br \/>\n      \/\/ \u5904\u7406\u9519\u8bef<br \/>\n    }<br \/>\n  }<br \/>\n} <\/p>\n<hr \/>\n<h5 id=\"%E6%80%BB%E7%BB%93\">\u603b\u7ed3<\/h5>\n<ul>\n<li>Actions \u662f\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u548c\u590d\u6742\u4e1a\u52a1\u903b\u8f91\u7684\u4e2d\u5fc3\u3002<\/li>\n<li>Actions \u901a\u8fc7\u00a0context.commit\u00a0\u6765\u63d0\u4ea4 Mutations \u4ee5\u4fee\u6539\u72b6\u6001\u3002<\/li>\n<li>\u4f7f\u7528\u00a0store.dispatch\u00a0(\u6216\u00a0this.$store.dispatch\u00a0\/\u00a0mapActions) \u6765\u89e6\u53d1 Actions\u3002<\/li>\n<li>Actions \u652f\u6301\u8f7d\u8377\u548c\u5bf9\u8c61\u98ce\u683c\u7684\u5206\u53d1\u3002<\/li>\n<li>Actions \u53ef\u4ee5\u8fd4\u56de Promise&#xff0c;\u4fbf\u4e8e\u5904\u7406\u5f02\u6b65\u6d41\u7a0b\u3002<\/li>\n<\/ul>\n<p>\u9075\u5faa &#034;Actions \u5904\u7406\u903b\u8f91&#xff0c;Mutations \u4fee\u6539\u72b6\u6001&#034; \u7684\u6a21\u5f0f&#xff0c;\u662f\u6784\u5efa\u53ef\u7ef4\u62a4\u3001\u53ef\u8c03\u8bd5\u7684 Vuex \u5e94\u7528\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p>\u00a0\u5199\u5728\u6700\u540e.\u00a0<\/p>\n<p>\u80fd\u5165\u624b\u4e00\u4e2a\u53ef\u4ee5\u7684\u9879\u76ee. \u6846\u67b6\u662f\u57fa\u7840. \u5e95\u5c42\u7684js&#043;html&#043;css \u4e5f\u9700\u8981\u6253\u7262.<\/p>\n<p>\u535a\u4e3b\u4e5f\u5b8c\u7ed3\u4e86 \u524d\u7aef \u57fa\u7840\u8bfe\u7a0b .\u53c2\u8003\u00a0https:\/\/blog.csdn.net\/chxii\/category_12913839.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb384\u6b21\uff0c\u70b9\u8d5e12\u6b21\uff0c\u6536\u85cf10\u6b21\u3002state: {count: 0},\/\/ \u4fee\u6539\u72b6\u6001},\/\/ \u4f7f\u7528\u8f7d\u8377\u6765\u4fee\u6539\u72b6\u6001})\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a mutations:increment\u548cdecrement\u3002increment\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff0c\u800cdecrement\u63a5\u53d7\u4e00\u4e2a\u5305\u542bamount\u5c5e\u6027\u7684\u5bf9\u8c61\u4f5c\u4e3a\u5176\u8f7d\u8377\u3002\u5728 Vuex store \u7684actions\u9009\u9879\u4e2d\u5b9a\u4e49 Actions\u3002\u6bcf\u4e2a Action \u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u540d\u4e3acontext\u7684\u5bf9\u8c61\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\u8fd9\u4e2acontextstate: \u6307\u5411\u3002rootState\u3002<\/p>\n","protected":false},"author":2,"featured_media":52883,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[684,187,625,87],"topic":[],"class_list":["post-52885","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-vue3","tag-javascript","tag-vue-js","tag-87"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>3.6 \u4fee\u6539vuex\u7684\u72b6\u6001Mutations ,Actions - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/52885.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"3.6 \u4fee\u6539vuex\u7684\u72b6\u6001Mutations ,Actions - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb384\u6b21\uff0c\u70b9\u8d5e12\u6b21\uff0c\u6536\u85cf10\u6b21\u3002state: {count: 0},\/\/ \u4fee\u6539\u72b6\u6001},\/\/ \u4f7f\u7528\u8f7d\u8377\u6765\u4fee\u6539\u72b6\u6001})\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a mutations:increment\u548cdecrement\u3002increment\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff0c\u800cdecrement\u63a5\u53d7\u4e00\u4e2a\u5305\u542bamount\u5c5e\u6027\u7684\u5bf9\u8c61\u4f5c\u4e3a\u5176\u8f7d\u8377\u3002\u5728 Vuex store \u7684actions\u9009\u9879\u4e2d\u5b9a\u4e49 Actions\u3002\u6bcf\u4e2a Action \u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u540d\u4e3acontext\u7684\u5bf9\u8c61\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\u8fd9\u4e2acontextstate: \u6307\u5411\u3002rootState\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/52885.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-11T12:54:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811125412-6899e7f41dd15.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52885.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/52885.html\",\"name\":\"3.6 \u4fee\u6539vuex\u7684\u72b6\u6001Mutations ,Actions - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-11T12:54:13+00:00\",\"dateModified\":\"2025-08-11T12:54:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/52885.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/52885.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/52885.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"3.6 \u4fee\u6539vuex\u7684\u72b6\u6001Mutations ,Actions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"3.6 \u4fee\u6539vuex\u7684\u72b6\u6001Mutations ,Actions - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/52885.html","og_locale":"zh_CN","og_type":"article","og_title":"3.6 \u4fee\u6539vuex\u7684\u72b6\u6001Mutations ,Actions - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb384\u6b21\uff0c\u70b9\u8d5e12\u6b21\uff0c\u6536\u85cf10\u6b21\u3002state: {count: 0},\/\/ \u4fee\u6539\u72b6\u6001},\/\/ \u4f7f\u7528\u8f7d\u8377\u6765\u4fee\u6539\u72b6\u6001})\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a mutations:increment\u548cdecrement\u3002increment\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\uff0c\u800cdecrement\u63a5\u53d7\u4e00\u4e2a\u5305\u542bamount\u5c5e\u6027\u7684\u5bf9\u8c61\u4f5c\u4e3a\u5176\u8f7d\u8377\u3002\u5728 Vuex store \u7684actions\u9009\u9879\u4e2d\u5b9a\u4e49 Actions\u3002\u6bcf\u4e2a Action \u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u540d\u4e3acontext\u7684\u5bf9\u8c61\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\u8fd9\u4e2acontextstate: \u6307\u5411\u3002rootState\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/52885.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-11T12:54:13+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250811125412-6899e7f41dd15.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/52885.html","url":"https:\/\/www.wsisp.com\/helps\/52885.html","name":"3.6 \u4fee\u6539vuex\u7684\u72b6\u6001Mutations ,Actions - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-11T12:54:13+00:00","dateModified":"2025-08-11T12:54:13+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/52885.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/52885.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/52885.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"3.6 \u4fee\u6539vuex\u7684\u72b6\u6001Mutations ,Actions"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/52885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=52885"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/52885\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/52883"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=52885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=52885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=52885"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=52885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}