{"id":968,"date":"2022-03-16T14:49:06","date_gmt":"2022-03-16T06:49:06","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=968"},"modified":"2022-03-16T14:49:06","modified_gmt":"2022-03-16T06:49:06","slug":"vue-vuex%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=968","title":{"rendered":"vue-vuex(\u5b66\u4e60\u7b14\u8bb0)"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"701\" height=\"551\" src=\"http:\/\/blog.xrhoujie.com\/wp-content\/uploads\/2022\/03\/3.png\" alt=\"\" class=\"wp-image-969\" srcset=\"http:\/\/blog.xrhoujie.com\/wp-content\/uploads\/2022\/03\/3.png 701w, http:\/\/blog.xrhoujie.com\/wp-content\/uploads\/2022\/03\/3-300x236.png 300w\" sizes=\"(max-width: 701px) 100vw, 701px\" \/><\/figure>\n\n\n\n<p>state\u7528\u4e8e\u521d\u59cb\u5316\u6570\u636e\u5185\u5bb9<\/p>\n\n\n\n<p>VueComponents\u54cd\u5e94\u7ec4\u4ef6\u901a\u8fc7 dispatch\u53d1\u5c04\u4e8b\u4ef6\u5230action\u8fdb\u884c\u4e8b\u4ef6\u54cd\u5e94\uff0c<\/p>\n\n\n\n<p>\u4f20\u9012\u5230Actions\u903b\u8f91\u5904\u7406\u540e\u901a\u8fc7commit\u5230Mutations\u8fdb\u884c\u8ba1\u7b97<\/p>\n\n\n\n<p>\u6700\u540e\u901a\u8fc7tutate\u901a\u77e5state\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Count.vue<\/strong>\n------\n&lt;template>\n    &lt;div>\n        &lt;h1>\u5f53\u524d\u6c42\u548c\u4e3a\uff1a{{sum}}&lt;\/h1>\n        &lt;h3>\u5f53\u524d\u6c42\u548c\u653e\u592710\u500d\u4e3a\uff1a{{bigSum}}&lt;\/h3>\n        &lt;h3>\u6211\u5728{{school}}\uff0c\u5b66\u4e60{{subject}}&lt;\/h3>\n     \n        &lt;button @click=\"increment(n)\">+&lt;\/button>\n        &lt;button @click=\"decrement(n)\">-&lt;\/button>\n        &lt;button @click=\"incrementOdd(n)\">\u5f53\u524d\u6c42\u548c\u4e3a\u5947\u6570\u518d\u52a0&lt;\/button>\n        &lt;button @click=\"incrementWait(n)\">\u7b49\u4e00\u7b49\u518d\u52a0&lt;\/button>\n    &lt;\/div>\n&lt;\/template>\n\n&lt;script>\n    import {mapState,mapGetters,mapMutations,mapActions} from 'vuex'\n    export default {\n        name:'Count',\n        data() {\n            return {\n                n:1, \/\/\u7528\u6237\u9009\u62e9\u7684\u6570\u5b57\n            }\n        },\n        computed:{\n            \/\/\u501f\u52a9mapState\u751f\u6210\u8ba1\u7b97\u5c5e\u6027\uff0c\u4ecestate\u4e2d\u8bfb\u53d6\u6570\u636e\u3002\uff08\u6570\u7ec4\u5199\u6cd5\uff09\n            ...mapState('countAbout',&#91;'sum','school','subject']),\n            ...mapState('personAbout',&#91;'personList']),\n            \/\/\u501f\u52a9mapGetters\u751f\u6210\u8ba1\u7b97\u5c5e\u6027\uff0c\u4ecegetters\u4e2d\u8bfb\u53d6\u6570\u636e\u3002\uff08\u6570\u7ec4\u5199\u6cd5\uff09\n            ...mapGetters('countAbout',&#91;'bigSum'])\n        },\n        methods: {\n            \/\/\u501f\u52a9mapMutations\u751f\u6210\u5bf9\u5e94\u7684\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4e2d\u4f1a\u8c03\u7528commit\u53bb\u8054\u7cfbmutations(\u5bf9\u8c61\u5199\u6cd5)\n            ...mapMutations('countAbout',{increment:'JIA',decrement:'JIAN'}),\n            \/\/\u501f\u52a9mapActions\u751f\u6210\u5bf9\u5e94\u7684\u65b9\u6cd5\uff0c\u65b9\u6cd5\u4e2d\u4f1a\u8c03\u7528dispatch\u53bb\u8054\u7cfbactions(\u5bf9\u8c61\u5199\u6cd5)\n            ...mapActions('countAbout',{incrementOdd:'jiaOdd',incrementWait:'jiaWait'})\n        },\n        mounted() {\n            console.log(this.$store)\n        },\n    }\n&lt;\/script>\n\n&lt;style lang=\"css\">\n    button{\n        margin-left: 5px;\n    }\n&lt;\/style>\n\n<strong>store\/index.js<\/strong>\n----\n\/\/\u8be5\u6587\u4ef6\u7528\u4e8e\u521b\u5efaVuex\u4e2d\u6700\u4e3a\u6838\u5fc3\u7684store\nimport Vue from 'vue'\n\/\/\u5f15\u5165Vuex\nimport Vuex from 'vuex'\nimport countOptions from '.\/count'\nimport personOptions from '.\/person'\n\/\/\u5e94\u7528Vuex\u63d2\u4ef6\nVue.use(Vuex)\n\n\/\/\u521b\u5efa\u5e76\u66b4\u9732store\nexport default new Vuex.Store({\n    modules:{\n        countAbout:countOptions,\n        personAbout:personOptions\n    }\n})\n\n<strong>store\/count.js<\/strong>\n----\n\/\/\u6c42\u548c\u76f8\u5173\u7684\u914d\u7f6e\nexport default {\n    namespaced:true,\n    actions:{\n        jiaOdd(context,value){\n            console.log('actions\u4e2d\u7684jiaOdd\u88ab\u8c03\u7528\u4e86')\n            if(context.state.sum % 2){\n                context.commit('JIA',value)\n            }\n        },\n        jiaWait(context,value){\n            console.log('actions\u4e2d\u7684jiaWait\u88ab\u8c03\u7528\u4e86')\n            setTimeout(()=>{\n                context.commit('JIA',value)\n            },500)\n        }\n    },\n    mutations:{\n        JIA(state,value){\n            console.log('mutations\u4e2d\u7684JIA\u88ab\u8c03\u7528\u4e86')\n            state.sum += value\n        },\n        JIAN(state,value){\n            console.log('mutations\u4e2d\u7684JIAN\u88ab\u8c03\u7528\u4e86')\n            state.sum -= value\n        },\n    },\n    state:{\n        sum:0, \/\/\u5f53\u524d\u7684\u548c\n        school:'\u6211\u7684\u5b66\u6821',\n        subject:'\u524d\u7aef',\n    },\n    getters:{\n        bigSum(state){\n            return state.sum*10\n        }\n    },\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>state\u7528\u4e8e\u521d\u59cb\u5316\u6570\u636e\u5185\u5bb9 VueComponents\u54cd\u5e94\u7ec4\u4ef6\u901a\u8fc7 dispatch\u53d1\u5c04\u4e8b\u4ef6\u5230action\u8fdb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[41,1],"tags":[38],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/968"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=968"}],"version-history":[{"count":1,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/968\/revisions"}],"predecessor-version":[{"id":970,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/968\/revisions\/970"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=968"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}