{"id":5809,"date":"2025-05-30T13:57:29","date_gmt":"2025-06-04T06:19:29","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5809"},"modified":"2025-06-04T13:57:29","modified_gmt":"2025-06-04T06:19:29","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8vue%e4%b8%ad%e4%bd%bf%e7%94%a8vuex%e9%80%b2%e8%a1%8c%e7%8b%80%e6%85%8b%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/vue-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%9c%a8vue%e4%b8%ad%e4%bd%bf%e7%94%a8vuex%e9%80%b2%e8%a1%8c%e7%8b%80%e6%85%8b%e7%ae%a1%e7%90%86\/","title":{"rendered":"\u6df1\u5165\u63a2\u7d22 Vuex\uff1a\u5728 Vue \u61c9\u7528\u4e2d\u5be6\u73fe\u9ad8\u6548\u72c0\u614b\u7ba1\u7406\u7684\u7d42\u6975\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Vue, Vuex, \u72b6\u6001\u7ba1\u7406, Vue \u5e94\u7528, \u524d\u7aef\u5f00\u53d1\"><\/p>\n<h1>\u6df1\u5165\u63a2\u7d22 Vuex\uff1a\u5728 Vue \u61c9\u7528\u4e2d\u5be6\u73fe\u9ad8\u6548\u72c0\u614b\u7ba1\u7406\u7684\u7d42\u6975\u6307\u5357<\/h1>\n<p>Vuex \u662f\u4e00\u500b\u5c08\u70ba Vue.js \u8a2d\u8a08\u7684\u72c0\u614b\u7ba1\u7406\u5eab\uff0c\u63d0\u4f9b\u4e86\u4e00\u500b\u96c6\u4e2d\u5f0f\u7684\u5b58\u5132\uff0c\u4f7f\u5f97\u5927\u578b\u61c9\u7528\u7a0b\u5e8f\u4e2d\u7684\u72c0\u614b\u7ba1\u7406\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u548c\u9ad8\u6548\u3002\u7121\u8ad6\u662f\u5171\u4eab\u6578\u64da\u9084\u662f\u8ffd\u8e64\u72c0\u614b\u8b8a\u5316\uff0cVuex \u90fd\u80fd\u8f15\u9b06\u61c9\u5c0d\u3002\u672c\u6587\u5c07\u6307\u5c0e\u60a8\u5982\u4f55\u5728 Vue \u4e2d\u6709\u6548\u4f7f\u7528 Vuex \u9032\u884c\u72c0\u614b\u7ba1\u7406\uff0c\u4e26\u63a2\u8a0e\u6700\u65b0\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u5b89\u88dd Vuex<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5148\u5b89\u88dd Vuex\u3002\u60a8\u53ef\u4ee5\u9078\u64c7\u4f7f\u7528 npm \u6216 yarn \u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<p>&#8220;`bash<br \/>\nnpm install vuex<br \/>\n&#8220;`<\/p>\n<p>\u6216\u8005\uff0c\u60a8\u4e5f\u53ef\u4ee5\u901a\u904e CDN \u4f86\u8f09\u5165 Vuex\uff1a<\/p>\n<p>&#8220;`html<br \/>\n<script src=\"https:\/\/unpkg.com\/vuex@4.0.0\/dist\/vuex.js\"><\/script><br \/>\n&#8220;`<\/p>\n<h2>\u5275\u5efa Store<\/h2>\n<p>Store \u662f Vuex \u7684\u6838\u5fc3\uff0c\u4f5c\u70ba\u61c9\u7528\u7a0b\u5e8f\u72c0\u614b\u7684\u96c6\u4e2d\u7ba1\u7406\u4e2d\u5fc3\u3002\u5275\u5efa Store \u5c0d\u8c61\u6642\uff0c\u60a8\u9700\u8981\u5b9a\u7fa9\u61c9\u7528\u7a0b\u5e8f\u7684\u521d\u59cb\u72c0\u614b\uff1a<\/p>\n<p>&#8220;`javascript<br \/>\nconst store = new Vuex.Store({<br \/>\n  state: {<br \/>\n    count: 0,<br \/>\n    user: {<br \/>\n      name: &#8216;John&#8217;,<br \/>\n      age: 30<br \/>\n    }<br \/>\n  }<br \/>\n});<br \/>\n&#8220;`<\/p>\n<h2>\u5b9a\u7fa9 Mutations<\/h2>\n<p>Mutations \u662f\u66f4\u6539 Store \u4e2d\u72c0\u614b\u7684\u552f\u4e00\u65b9\u5f0f\uff0c\u6bcf\u500b Mutation \u90fd\u662f\u540c\u6b65\u51fd\u6578\u3002\u60a8\u53ef\u4ee5\u5b9a\u7fa9\u591a\u500b Mutations \u4f86\u7ba1\u7406\u72c0\u614b\u8b8a\u5316\uff1a<\/p>\n<p>&#8220;`javascript<br \/>\nconst store = new Vuex.Store({<br \/>\n  state: {<br \/>\n    count: 0<br \/>\n  },<br \/>\n  mutations: {<br \/>\n    increment(state, payload) {<br \/>\n      state.count += payload;<br \/>\n    },<br \/>\n    decrement(state, payload) {<br \/>\n      state.count -= payload;<br \/>\n    }<br \/>\n  }<br \/>\n});<br \/>\n&#8220;`<\/p>\n<h2>\u5b9a\u7fa9 Actions<\/h2>\n<p>Actions \u7528\u65bc\u8655\u7406\u7570\u6b65\u64cd\u4f5c\uff0c\u5b83\u5011\u53ef\u4ee5\u8abf\u7528 Mutations\u3002\u6bcf\u500b Action \u90fd\u53ef\u4ee5\u63a5\u53d7\u4e0a\u4e0b\u6587\u5c0d\u8c61\u548c\u6709\u6548\u8f09\u8377\u4f5c\u70ba\u53c3\u6578\uff1a<\/p>\n<p>&#8220;`javascript<br \/>\nconst store = new Vuex.Store({<br \/>\n  state: {<br \/>\n    count: 0<br \/>\n  },<br \/>\n  actions: {<br \/>\n    increment(context, payload) {<br \/>\n      context.commit(&#8216;increment&#8217;, payload);<br \/>\n    },<br \/>\n    decrement(context, payload) {<br \/>\n      context.commit(&#8216;decrement&#8217;, payload);<br \/>\n    }<br \/>\n  }<br \/>\n});<br \/>\n&#8220;`<\/p>\n<h2>\u5c07 Store \u6dfb\u52a0\u5230 Vue \u5be6\u4f8b<\/h2>\n<p>\u6700\u5f8c\uff0c\u60a8\u9700\u8981\u5c07 Store \u5be6\u4f8b\u8207 Vue \u61c9\u7528\u7d81\u5b9a\uff0c\u4ee5\u4fbf\u5728\u7d44\u4ef6\u4e2d\u4f7f\u7528\u5b83\uff1a<\/p>\n<p>&#8220;`javascript<br \/>\nconst app = new Vue({<br \/>\n  el: &#8216;#app&#8217;,<br \/>\n  store<br \/>\n});<br \/>\n&#8220;`<\/p>\n<p>\u73fe\u5728\uff0c\u60a8\u53ef\u4ee5\u5728\u61c9\u7528\u4e2d\u8f15\u9b06\u8a2a\u554f Store \u7684\u72c0\u614b\uff1a<\/p>\n<p>&#8220;`html<br \/>\n{{ <span class=\"katex math inline\">store.state.count }}<br \/>\n&#8220;`<\/p>\n<p>\u4ee5\u53ca\u901a\u904e Actions \u66f4\u65b0\u72c0\u614b\uff1a<\/p>\n<p>&#8220;`javascript<br \/>\nthis.<\/span>store.dispatch(&#8216;increment&#8217;, 10);<br \/>\n&#8220;`<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Vuex \u662f\u4e00\u500b\u5f37\u5927\u7684\u72c0\u614b\u7ba1\u7406\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u60a8\u5728\u5927\u578b Vue \u61c9\u7528\u4e2d\u9ad8\u6548\u5730\u7ba1\u7406\u72c0\u614b\u3002\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u5b89\u88dd Vuex\u3001\u5275\u5efa Store\u3001\u5b9a\u7fa9 Mutations \u548c Actions\uff0c\u4ee5\u53ca\u5c07 Store \u6574\u5408\u5230 Vue \u5be6\u4f8b\u4e2d\u3002\u901a\u904e\u9019\u4e9b\u6b65\u9a5f\uff0c\u60a8\u5c07\u80fd\u5920\u66f4\u597d\u5730\u7d44\u7e54\u548c\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aVuex\u662f\u4e00\u500b\u57fa\u65bcVue.js\u7684\u72b6\u6001\u7ba1\u7406\u6a21\u5f0f\uff0c\u5b83\u53ef\u4ee5\u8b93\u60a8\u5728\u591a\u500b\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u72c0\u614b\uff0c\u4e26\u63d0\u4f9b\u4e00\u500b\u53ef\u9810\u6e2c\u7684\u6578\u64da\u6d41\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728Vue\u4e2d\u4f7f\u7528Vuex\u9032\u884c\u72c0\u614b\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[180,179],"tags":[178,177],"class_list":["post-5809","post","type-post","status-publish","format-standard","hentry","category-vue","category-vue-js","tag-vue","tag-vue-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1vH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5809"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5809\/revisions"}],"predecessor-version":[{"id":5810,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5809\/revisions\/5810"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}