{"id":1018,"date":"2023-03-12T09:28:52","date_gmt":"2023-03-12T01:28:52","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1018"},"modified":"2023-04-29T13:15:14","modified_gmt":"2023-04-29T05:15:14","slug":"summary-of-methods-for-obtaining-vuex-status-states-in-vue-components","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/summary-of-methods-for-obtaining-vuex-status-states-in-vue-components\/","title":{"rendered":"Vue\u7ec4\u4ef6\u4e2d\u83b7\u5f97Vuex\u72b6\u6001state\u7684\u65b9\u6cd5\u6c47\u603b"},"content":{"rendered":"<p>Vuex\u4f7f\u7528\u5355\u4e00\u72b6\u6001\u6811\uff08\u4e00\u4e2a\u5bf9\u8c61\u5c31\u5305\u542b\u4e86\u5168\u90e8\u7684\u5e94\u7528\u5c42\u7ea7\u72b6\u6001\uff09\uff0c\u5b83\u4f5c\u4e3a\u552f\u4e00\u6570\u636e\u6e90\u5b58\u5728\uff0c\u6bcf\u4e2a\u5e94\u7528\u4ec5\u4ec5\u6709\u4e00\u4e2astore\u5b9e\u4f8b\u3002\u5355\u4e00\u72b6\u6001\u6811\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u76f4\u63a5\u5b9a\u4f4d\u4efb\u4e00\u7279\u5b9a\u7684\u72b6\u6001\u7247\u6bb5\uff0c\u5728\u8c03\u8bd5\u8fc7\u7a0b\u4e2d\u4e5f\u80fd\u8f7b\u6613\u5730\u53d6\u5f97\u6574\u4e2a\u5f53\u524d\u5e94\u7528\u72b6\u6001\u7684\u5feb\u7167\u3002<\/p>\n<p>Vuex\u7684\u72b6\u6001\u5b58\u50a8\u662f\u54cd\u5e94\u5f0f\u7684\u3002\u5728Vue\u7ec4\u4ef6\u4e2d\u83b7\u53d6Vuex\u72b6\u6001\u603b\u5171\u6709\u4ee5\u4e0b5\u79cd\u53ef\u884c\u7684\u65b9\u6cd5\u3002<\/p>\n<p><!-- more --><\/p>\n<h3>\u4ecestore\u5b9e\u4f8b\u4e2d\u8bfb\u53d6\u72b6\u6001\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u5c31\u662f\u5728\u8ba1\u7b97\u5c5e\u6027\u4e2d\u8fd4\u56de\u67d0\u4e2a\u72b6\u6001\uff08\u9700\u8981\u5bfc\u5165store\u7ec4\u4ef6\uff09<\/h3>\n<pre><code class=\"language-javascript\">import store from &#039;..\/..\/store\/index.js&#039;\n\nconst Counter = {\n  template: `&lt;div&gt;{{ count }}&lt;\/div&gt;`,\n  computed: {\n    count() {\n      return store.state.count \/\/ \u8fd4\u56destore\u5b9e\u4f8b\u7684count\u72b6\u6001\n    }\n  }\n}<\/code><\/pre>\n<p>\u6bcf\u5f53store.state.count\u53d1\u751f\u53d8\u5316\uff0c\u90fd\u4f1a\u91cd\u65b0\u6c42\u53d6\u8ba1\u7b97\u5c5e\u6027\uff0c\u5e76\u4e14\u89e6\u53d1\u66f4\u65b0\u76f8\u5173\u8054\u7684DOM<\/p>\n<p>\u7f3a\u70b9\uff1a\u8fd9\u79cd\u6a21\u5f0f\u5bfc\u81f4\u7ec4\u4ef6\u4f9d\u8d56\u5168\u5c40\u72b6\u6001\u5355\u4f8b\u3002\u5728\u6bcf\u4e2a\u9700\u8981state\u7684\u7ec4\u4ef6\u4e2d\u9700\u8981\u9891\u7e41\u5730\u5bfc\u5165\uff0c\u5e76\u4e14\u5728\u6d4b\u8bd5\u7ec4\u4ef6\u65f6\u9700\u8981\u6a21\u62df\u72b6\u6001\u3002<\/p>\n<p>Vuex\u901a\u8fc7store\u9009\u9879\uff0c\u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\u5c06\u72b6\u6001\u4ece\u6839\u7ec4\u4ef6\u6ce8\u5165\u5230\u6bcf\u4e00\u4e2a\u5b50\u7ec4\u4ef6\u4e2d\uff0c\u524d\u63d0\u662f\u8981\u8c03\u7528<code>Vue.use(Vuex)<\/code><\/p>\n<pre><code class=\"language-javascript\">Vue.use(Vuex)\n\nconst app = new Vue({\n  el: &#039;#app&#039;,\n  store, \/\/\u6839\u7ec4\u4ef6\u901a\u8fc7store\u9009\u9879\u5c06store\u5b9e\u4f8b\u6ce8\u5165\u6240\u6709\u5730\u5b50\u7ec4\u4ef6\n  components: { Counter },\n  template:`\n    &lt;div class=&quot;app&quot;&gt;\n     &lt;Counter&gt;&lt;Counter&gt;\n    &lt;\/div&gt;\n  `\n})<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u80fd\u591f\u8ba9\u5b50\u7ec4\u4ef6\u901a\u8fc7this.$store\u8bbf\u95ee\u5230store\u5b9e\u4f8b\u3002<\/p>\n<h3>\u6839\u7ec4\u4ef6\u6ce8\u518cstore\u540e\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u7684\u5b9e\u73b0\u6765\u8bbf\u95eestore\uff0c\u800c\u4e0d\u7528\u9891\u7e41\u5bfc\u5165state\u7684\u7ec4\u4ef6<\/h3>\n<pre><code class=\"language-javascript\">const Counter = {\n  template: `&lt;div&gt;{{ counter }}&lt;\/div&gt;`,\n  computed: {\n    count () {\n      return this.$state.count\n    }\n  }\n}<\/code><\/pre>\n<h3>mapState\u8f85\u52a9\u51fd\u6570\uff08\u7528\u4e8e\u83b7\u53d6\u591a\u4e2astate\u72b6\u6001\uff09<\/h3>\n<p>\u5f53\u4e00\u4e2a\u7ec4\u4ef6\u9700\u8981\u83b7\u53d6\u591a\u4e2a\u72b6\u6001\u65f6\uff0c\u5c06\u8fd9\u4e9b\u72b6\u6001\u90fd\u58f0\u660e\u4e3a\u8ba1\u7b97\u5c5e\u6027\u4f1a\u6709\u4e9b\u91cd\u590d\u548c\u5197\u4f59\uff08\u5982\u4e0a\u9762\u4ee3\u7801\u7684count\u51fd\u6570\uff09\uff0c\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u7528mapState\u8f85\u52a9\u51fd\u6570\u751f\u6210\u8ba1\u7b97\u5c5e\u6027\u3002<\/p>\n<pre><code class=\"language-javascript\">import { mapState } from &#039;vuex&#039;\nexport default {\n  computed: mapState({\n    \/\/\u65b9\u5f0f\u4e00\uff1a\u7bad\u5934\u51fd\u6570\n    count: state =&gt; state.count,\n    \/\/\u65b9\u5f0f\u4e8c\uff1a\u4f20\u5b57\u7b26\u4e32\u53c2\u6570\n    countAlias: &#039;count&#039;,\n    \/\/\u5982\u679c\u9700\u8981\u4f7f\u7528this\u83b7\u53d6\u5c40\u90e8\u72b6\u6001\uff0c\u5c31\u8981\u4f7f\u7528\u5e38\u89c4\u51fd\u6570\n    countPlusLocalState (state) {\n      return state.count + this.localCount\n    }\n  })\n}<\/code><\/pre>\n<h3>\u5982\u679c\u6620\u5c04\u7684\u8ba1\u7b97\u5c5e\u6027\u540d\u79f0\u4e0estate\u7684\u5b50\u8282\u70b9\u76f8\u540c\uff0c\u6211\u4eec\u7ed9mapState\u4f20\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4<\/h3>\n<pre><code class=\"language-javascript\">computed: mapState([\n  &#039;count&#039;\n])<\/code><\/pre>\n<h3>\u5bf9\u8c61\u5c55\u5f00\u8fd0\u7b97\u7b26<\/h3>\n<p>\u4ee5\u4e0a\u90fd\u662fmapState\u5355\u72ec\u4f7f\u7528\u5728computed\u5c5e\u6027\u4e2d\uff0c\u4f46\u662f\u5982\u679c\u8981\u548c\u666e\u901a\u7684\u5c40\u90e8\u8ba1\u7b97\u5c5e\u6027\u6df7\u5408\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u5bf9\u8c61\u5c55\u5f00\u8fd0\u7b97\u7b26(\u2026)<\/p>\n<pre><code class=\"language-javascript\">import { mapState, mapGetter } from &#039;vuex&#039;\n\nexport default {\n  methods: {\n    increment () {\n      this.$store.commit(&#039;increment&#039;);\n    }\n  },\n  computed: {\n    elsecomputed () {}, \/\/\u8fd9\u662f\u666e\u901a\u7684\u5c40\u90e8\u8ba1\u7b97\u5c5e\u6027\n    ...mapGetters([\n      &#039;count&#039;\n    ]),\n    ...mapState({\n      counts () {\n        return this.$store.state.count;\n      }\n    })\n  }\n}<\/code><\/pre>\n<p>\u5e76\u4e0d\u9700\u8981\u628a\u6240\u6709\u72b6\u6001\u90fd\u653e\u5230Vuex\uff0c\u6709\u4e9b\u72b6\u6001\u4e25\u683c\u5c5e\u4e8e\u5355\u4e2a\u7ec4\u4ef6\uff0c\u6700\u597d\u662f\u4f5c\u4e3a\u7ec4\u4ef6\u7684\u5c40\u90e8\u72b6\u6001\uff0c\u8981\u6839\u636e\u5e94\u7528\u5f00\u53d1\u8fdb\u884c\u6743\u8861\u548c\u786e\u5b9a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vuex\u4f7f\u7528\u5355\u4e00\u72b6\u6001\u6811\uff08\u4e00\u4e2a\u5bf9\u8c61\u5c31\u5305\u542b\u4e86\u5168\u90e8\u7684\u5e94\u7528\u5c42\u7ea7\u72b6\u6001\uff09\uff0c\u5b83\u4f5c\u4e3a\u552f\u4e00\u6570\u636e\u6e90\u5b58\u5728\uff0c\u6bcf\u4e2a\u5e94\u7528\u4ec5\u4ec5\u6709\u4e00\u4e2astor [&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-1018","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\/1018","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=1018"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1018\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}