{"id":1015,"date":"2023-03-12T09:21:59","date_gmt":"2023-03-12T01:21:59","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1015"},"modified":"2023-04-29T13:16:25","modified_gmt":"2023-04-29T05:16:25","slug":"analysis-of-vuex-core-concepts-state-getters-mutations-actions","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/analysis-of-vuex-core-concepts-state-getters-mutations-actions\/","title":{"rendered":"Vuex\u6838\u5fc3\u6982\u5ff5\u89e3\u6790State Getters Mutations Actions"},"content":{"rendered":"<h2>Vuex\u7b80\u4ecb<\/h2>\n<p>Vuex\u662f\u89e3\u51b3Vue\u7ec4\u4ef6\u548c\u7ec4\u4ef6\u95f4\u76f8\u4e92\u901a\u4fe1\u800c\u5b58\u5728\u7684\u3002<\/p>\n<p>\u5b89\u88c5\uff1a<\/p>\n<pre><code>npm install --save vuex<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u5f15\u5165\uff1a<\/p>\n<pre><code class=\"language-javascript\">import Vuex from &#039;vuex&#039;\nimport Vue from &#039;vue&#039;\nVue.use(Vuex)<\/code><\/pre>\n<p>Vuex\u53c2\u6570\u4ecb\u7ecd<\/p>\n<ul>\n<li>State\uff1a\u50a8\u5b58\u521d\u59cb\u5316\u6570\u636e<\/li>\n<li>Getters\uff1a\u5bf9State\u91cc\u9762\u7684\u6570\u636e\u4e8c\u6b21\u5904\u7406\uff08\u5bf9\u6570\u636e\u8fdb\u884c\u8fc7\u6ee4\u7c7b\u4f3cfilter\u7684\u4f5c\u7528\uff09\u3002\u6bd4\u5982State\u8fd4\u56de\u7684\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6211\u4eec\u60f3\u53d6\u5bf9\u8c61\u4e2d\u4e00\u4e2a\u952e\u7684\u503c\u7528\u8fd9\u4e2a\u65b9\u6cd5<\/li>\n<li>Mutations\uff1a\u5bf9\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\u7684\u65b9\u6cd5\u5168\u90e8\u5199\u5728\u91cc\u9762\uff08\u7c7b\u4f3ccomputed\uff09\u3002\u5728\u9875\u9762\u4e2d\u89e6\u53d1\u65f6\u4f7f\u7528<code>this.$store.commit(&#039;mutationName&#039;)<\/code>\uff0c\u89e6\u53d1Mutations\u65b9\u6cd5\u6539\u53d8state\u7684\u503c<\/li>\n<li>Actions\uff1a\u5904\u7406Mutations\u4e2d\u5df2\u7ecf\u5199\u597d\u7684\u65b9\u6cd5\uff0c\u5176\u76f4\u63a5\u89e6\u53d1\u65b9\u5f0f\u662f<code>this.$store.dispatch(actionName)<\/code><\/li>\n<\/ul>\n<p>\u6253\u5370Vuex<\/p>\n<pre><code class=\"language-javascript\">console.log(Vuex) \/\/Vuex\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u91cc\u9762\u5305\u542b\n\nVuex = {\n    Store:function Store(){},    \n    mapActions:function(){},  \/\/\u5bf9\u5e94Actions\u7684\u7ed3\u679c\u96c6\n    mapGetters:function(){},  \/\/\u5bf9\u5e94Getters\u7684\u7ed3\u679c\u96c6\n    mapMutations:function(){},  \/\/\u5bf9\u5e94Mutations\u7684\u7ed3\u679c\u96c6\n    mapState:function(){},  \/\/\u5bf9\u5e94State\u7684\u7ed3\u679c\u96c6\n    install:function install(){},  \/\/\u6682\u65f6\u4e0d\u505a\u8bb2\u89e3 \n    installed:true  \/\/\u6682\u65f6\u4e0d\u505a\u8bb2\u89e3\n}<\/code><\/pre>\n<p>\u5f15\u7528Vuex<\/p>\n<pre><code class=\"language-javascript\">import { mapState } from &#039;vuex&#039;;  \/\/\u5982\u679c\u6211\u4eec\u53ea\u9700\u8981\u91cc\u9762\u7684State\u65f6\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5199\nimport { mapGetters, mapMutations } from &#039;vuex&#039;;  \/\/\u5982\u679c\u9700\u8981\u5f15\u7528\u591a\u4e2a\u65f6\u7528\u8fd9\u79cd\u65b9\u5f0f\u5904\u7406<\/code><\/pre>\n<h2>State<\/h2>\n<p>State\u8d1f\u8d23\u5b58\u50a8\u6574\u4e2a\u5e94\u7528\u7684\u72b6\u6001\u6570\u636e\uff0c\u4e00\u822c\u9700\u8981\u5728\u4f7f\u7528\u65f6\u5728\u6839\u8282\u70b9\u6ce8\u5165store\u5bf9\u8c61\uff0c\u540e\u671f\u5c31\u53ef\u4ee5\u4f7f\u7528<code>this.$store.state<\/code>\u76f4\u63a5\u83b7\u53d6\u72b6\u6001<\/p>\n<pre><code class=\"language-javascript\">\/\/store\u4e3a\u5b9e\u4f8b\u5316\u751f\u6210\u7684\nimport store from &#039;.\/store&#039; \n\nnew Vue({\n  el: &#039;#app&#039;,\n  store,\n  render: h =&gt; h(App)\n})<\/code><\/pre>\n<p>\u8fd9\u4e2astore\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u5bb9\u5668\uff0c\u5305\u542b\u7740\u5e94\u7528\u4e2d\u7684state\u7b49\u3002\u5b9e\u4f8b\u5316\u751f\u6210store\u7684\u8fc7\u7a0b\u662f\uff1a\u3000\u3000<\/p>\n<pre><code class=\"language-javascript\">\/\/.\/store\u6587\u4ef6\nconst store = new Vuex.Store({\n  state: {  \/\/\u653e\u7f6estate\u7684\u503c\n    count: 0,\n    strLength: &quot;abcd1234&quot;\n  },\n  getters: {  \/\/\u653e\u7f6egetters\u65b9\u6cd5\n    strLength: state =&gt; state.aString.length\n  },\n  mutations: {  \/\/\u653e\u7f6emutations\u65b9\u6cd5\n    mutationName(state, data) {\n      \/\/\u5728\u8fd9\u91cc\u6539\u53d8state\u4e2d\u7684\u6570\u636e\n      state.count = 100;\n    }\n  },\n  \/\/\u5f02\u6b65\u7684\u6570\u636e\u64cd\u4f5c\n  actions: {  \/\/\u653e\u7f6eactions\u65b9\u6cd5\n    actionName({ commit }) {\n      \/\/dosomething\n      commit(&#039;mutationName&#039;)\n    },\n    getUser ({commit}, id) {\n      api.getUser(id).then(response =&gt; {\n        commit(&#039;mutationName&#039;, {&#039;httpCode&#039;: response.status, &#039;data&#039;: response.data})\n      }).catch((error) =&gt; {  \/\/ \u9519\u8bef\u5904\u7406\n        console.log(error);\n      });\n    }\n  }\n});\n\nexport default store;<\/code><\/pre>\n<p>\u540e\u7eed\u5728\u7ec4\u4ef6\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u60f3\u8981\u83b7\u53d6\u5bf9\u5e94\u7684\u72b6\u6001\u5373\u53ef\u76f4\u63a5\u4f7f\u7528<code>this.$store.state<\/code>\u83b7\u53d6\u3002\u5f53\u7136\uff0c\u4e5f\u53ef\u4ee5\u5229\u7528Vuex\u63d0\u4f9b\u7684mapState\u8f85\u52a9\u51fd\u6570\u5c06state\u6620\u5c04\u5230\u8ba1\u7b97\u5c5e\u6027\u4e2d\u53bb\uff0c\u5982<\/p>\n<pre><code class=\"language-javascript\">import {mapState} from &#039;vuex&#039;\n\nexport default {  \/\/\u7ec4\u4ef6\u4e2d\n  computed: mapState({\n    count: state =&gt; state.count\n  })\n}<\/code><\/pre>\n<h2>Getters<\/h2>\n<p>\u6709\u4e9b\u72b6\u6001\u9700\u8981\u505a\u4e8c\u6b21\u5904\u7406\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528getters\u3002\u901a\u8fc7<code>this.$store.getters.valueName<\/code>\u5bf9\u6d3e\u751f\u51fa\u6765\u7684\u72b6\u6001\u8fdb\u884c\u8bbf\u95ee\u3002\u6216\u8005\u76f4\u63a5\u4f7f\u7528\u8f85\u52a9\u51fd\u6570mapGetters\u5c06\u5176\u6620\u5c04\u5230\u672c\u5730\u8ba1\u7b97\u5c5e\u6027\u4e2d\u53bb\u3002<\/p>\n<p>\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u65b9\u5f0f<\/p>\n<pre><code class=\"language-javascript\">import {mapGetters} from &#039;vuex&#039;\n\nexport default {  \n  computed: mapGetters([\n    &#039;strLength&#039;\n  ])\n}<\/code><\/pre>\n<h2>Mutations<\/h2>\n<p>Mutations\u7684\u4e2d\u6587\u610f\u601d\u662f\u201c\u53d8\u5316\u201d\uff0c\u5229\u7528\u5b83\u53ef\u4ee5\u66f4\u6539\u72b6\u6001\uff0c\u672c\u8d28\u5c31\u662f\u7528\u6765\u5904\u7406\u6570\u636e\u7684\u51fd\u6570\u3002<code>store.commit(mutationName)<\/code>\u662f\u7528\u6765\u89e6\u53d1\u4e00\u4e2amutation\u7684\u65b9\u6cd5\u3002\u9700\u8981\u8bb0\u4f4f\u7684\u662f\uff0c\u5b9a\u4e49\u7684mutation\u5fc5\u987b\u662f\u540c\u6b65\u51fd\u6570\uff0c\u5426\u5219devtool\u4e2d\u7684\u6570\u636e\u5c06\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\uff0c\u4f7f\u72b6\u6001\u6539\u53d8\u53d8\u5f97\u96be\u4ee5\u8ddf\u8e2a\u3002<\/p>\n<p>\u5728\u7ec4\u4ef6\u4e2d\u89e6\u53d1\uff1a<\/p>\n<pre><code class=\"language-javascript\">export default {\n  methods: {\n    handleClick() {\n      this.$store.commit(&#039;mutationName&#039;)\n    }\n  }\n}<\/code><\/pre>\n<p>\u6216\u8005\u4f7f\u7528\u8f85\u52a9\u51fd\u6570mapMutations\u76f4\u63a5\u5c06\u89e6\u53d1\u51fd\u6570\u6620\u5c04\u5230methods\u4e0a\uff0c\u8fd9\u6837\u5c31\u80fd\u5728\u5143\u7d20\u4e8b\u4ef6\u7ed1\u5b9a\u4e0a\u76f4\u63a5\u4f7f\u7528\u4e86\u3002\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">import {mapMutations} from &#039;vuex&#039;\n\nexport default {\n  methods: mapMutations([\n    &#039;mutationName&#039;\n  ])\n}<\/code><\/pre>\n<h2>Actions<\/h2>\n<p>Actions\u4e5f\u53ef\u4ee5\u7528\u4e8e\u6539\u53d8\u72b6\u6001\uff0c\u4e0d\u8fc7\u662f\u901a\u8fc7\u89e6\u53d1mutation\u5b9e\u73b0\u7684\uff0c\u91cd\u8981\u7684\u662f\u53ef\u4ee5\u5305\u542b\u5f02\u6b65\u64cd\u4f5c\u3002\u5176\u8f85\u52a9\u51fd\u6570\u662fmapActions\u4e0emapMutations\u7c7b\u4f3c\uff0c\u4e5f\u662f\u7ed1\u5b9a\u5728\u7ec4\u4ef6\u7684methods\u4e0a\u7684\u3002\u5982\u679c\u9009\u62e9\u76f4\u63a5\u89e6\u53d1\u7684\u8bdd\uff0c\u4f7f\u7528<code>this.$store.dispatch(actionName)<\/code>\u65b9\u6cd5\u3002<\/p>\n<p>\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/p>\n<pre><code class=\"language-javascript\">import {mapActions} from &#039;vuex&#039;\n\nexport default {\n  methods: mapActions([\n    &#039;actionName&#039;,\n  ])\n}<\/code><\/pre>\n<h2>Plugins<\/h2>\n<p>\u63d2\u4ef6\u5c31\u662f\u4e00\u4e2a\u94a9\u5b50\u51fd\u6570\uff0c\u5728\u521d\u59cb\u5316store\u7684\u65f6\u5019\u5f15\u5165\u5373\u53ef\u3002\u6bd4\u8f83\u5e38\u7528\u7684\u662f\u5185\u7f6e\u7684logger\u63d2\u4ef6\uff0c\u7528\u4e8e\u4f5c\u4e3a\u8c03\u8bd5\u4f7f\u7528\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/\u5199\u5728.\/store\u6587\u4ef6\u4e2d\nimport createLogger from &#039;vuex\/dist\/logger&#039;\n\nconst store = Vuex.Store({\n  ...\n  plugins: [createLogger()]\n})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Vuex\u7b80\u4ecb Vuex\u662f\u89e3\u51b3Vue\u7ec4\u4ef6\u548c\u7ec4\u4ef6\u95f4\u76f8\u4e92\u901a\u4fe1\u800c\u5b58\u5728\u7684\u3002 \u5b89\u88c5\uff1a npm install &#8211;save [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[279],"class_list":["post-1015","post","type-post","status-publish","format-standard","hentry","category-vue","tag-vuex"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1015"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1015\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}