{"id":529,"date":"2021-08-16T15:52:49","date_gmt":"2021-08-16T07:52:49","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=529"},"modified":"2021-09-11T06:52:31","modified_gmt":"2021-09-10T22:52:31","slug":"vuex%e6%ba%90%e7%a0%81%e4%b9%8b%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e6%b3%a8%e5%85%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=529","title":{"rendered":"Vuex\u6e90\u7801\u4e4b\u5982\u4f55\u5b9e\u73b0\u6ce8\u5165\uff1f"},"content":{"rendered":"\n<p>Vuex \u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Vuex is a&nbsp;state management pattern + library&nbsp;for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.<\/p><\/blockquote>\n\n\n\n<p><br>Vuex \u662f\u4e00\u4e2a\u4e13\u4e3a Vue.js \u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u7684\u72b6\u6001\u7ba1\u7406\u6a21\u5f0f\u3002\u5b83\u91c7\u7528\u96c6\u4e2d\u5f0f\u5b58\u50a8\u7ba1\u7406\u5e94\u7528\u7684\u6240\u6709\u7ec4\u4ef6\u7684\u72b6\u6001\uff0c\u5e76\u4ee5\u76f8\u5e94\u7684\u89c4\u5219\u4fdd\u8bc1\u72b6\u6001\u4ee5\u4e00\u79cd\u53ef\u9884\u6d4b\u7684\u65b9\u5f0f\u53d1\u751f\u53d8\u5316\u3002<\/p>\n\n\n\n<p><br>\u4e00\u4e2a\u5e93\u3001\u6846\u67b6\u7684\u51fa\u73b0\u80af\u5b9a\u662f\u4e3a\u4e86\u89e3\u51b3\u67d0\u4e00\u79cd\u95ee\u9898\uff0cVuex \u4e5f\u4e0d\u4f8b\u5916\uff0c\u5b83\u4e3b\u8981\u89e3\u51b3\u4e86\u4ee5\u4e0b\u4e24\u79cd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u591a\u4e2a\u89c6\u56fe\u4f9d\u8d56\u4e8e\u540c\u4e00\u72b6\u6001\u3002<\/li><li>\u6765\u81ea\u4e0d\u540c\u89c6\u56fe\u7684\u884c\u4e3a\u9700\u8981\u53d8\u66f4\u540c\u4e00\u72b6\u6001\u3002<\/li><\/ul>\n\n\n\n<p>\u5b83\u662f\u628a\u7ec4\u4ef6\u7684\u5171\u4eab\u72b6\u6001\u62bd\u53d6\u51fa\u6765\uff0c\u4ee5\u4e00\u4e2a\u5168\u5c40\u5355\u4f8b\u6a21\u5f0f\u6765\u7ba1\u7406\u3002\u539f\u7406\u56fe\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"701\" height=\"551\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2021\/08\/image-1.jpeg\" alt=\"\" class=\"wp-image-531\" srcset=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2021\/08\/image-1.jpeg 701w, https:\/\/zhuxinyong.com\/wp-content\/uploads\/2021\/08\/image-1-300x236.jpeg 300w, https:\/\/zhuxinyong.com\/wp-content\/uploads\/2021\/08\/image-1-670x527.jpeg 670w\" sizes=\"auto, (max-width: 701px) 100vw, 701px\" \/><\/figure>\n\n\n\n<p>\u5f53\u7136 Vuex \u80af\u5b9a\u4e5f\u4e0d\u662f\u9002\u7528\u4e8e\u6240\u6709\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u5b83\u5e26\u6765\u4e86\u4e00\u4e9b\u5b83\u81ea\u5df1\u7684\u6982\u5ff5\uff1aState\u3001Getters\u3001Mutations\u3001Actions\u3001Modules \u3002\u82e5\u4f60\u7684\u9879\u76ee\u591f\u7b80\u5355\uff0c\u5efa\u8bae\u8fd8\u662f\u4e0d\u8981\u7528 Vuex\u3002<br>\u6e90\u7801\u5206\u6790\uff1a\u6211\u4eec\u4ece Vuex \u7684\u4f7f\u7528\u65b9\u6cd5\u5f00\u59cb\u5165\u624b\uff1a<br>\u521d\u59cb\u5316\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import Vue from 'vue'\nimport Vuex from 'vuex'\n\nVue.use(Vuex)\n\nconst store = new Vuex.Store({\n  state: {\n    count: 0\n  },\n  mutations: {\n    increment (state) {\n      state.count++\n    }\n  }\n})<\/pre>\n\n\n\n<p>\u4ece\u6839\u7ec4\u4ef6\u6ce8\u5165 store\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new Vue({\n  el: '#app',\n  store: store,\n})<\/pre>\n\n\n\n<p>\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u51fa\u662f\u901a\u8fc7 Vue.use \u6765\u4f7f\u7528 Vuex \u7684\u90a3 Vue.use \u662f\u4ec0\u4e48\u5462\uff1f\u4f5c\u7528\u4e3b\u8981\u662f\u6765\u4f7f\u7528 Vue \u7684\u63d2\u4ef6\u7684\u3002\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export function initUse (Vue: GlobalAPI) {\n  Vue.use = function (plugin: Function | Object) {\n    const installedPlugins = (this._installedPlugins || (this._installedPlugins = []))\n    \/\/ \u9632\u6b62\u591a\u6b21 use\n    if (installedPlugins.indexOf(plugin) > -1) {\n      return this\n    }\n\n    \/\/ additional parameters\n    const args = toArray(arguments, 1)\n    args.unshift(this)\n    \/\/ \u6839\u636e plugin \u7c7b\u578b\u7684\u4e0d\u540c\u6267\u884c\u4e0d\u540c\u903b\u8f91\u30021.\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u5219\u6267\u884c\u5b83\u8eab\u4e0a\u7684intall \u65b9\u6cd5\uff0c2. plugin \u51fd\u6570\u65f6\u5219\u76f4\u63a5\u6267\u884c\n    if (typeof plugin.install === 'function') {\n      plugin.install.apply(plugin, args)\n    } else if (typeof plugin === 'function') {\n      plugin.apply(null, args)\n    }\n    installedPlugins.push(plugin)\n    return this\n  }\n}<\/pre>\n\n\n\n<p>\u90a3 Vuex \u65e2\u7136\u662f\u901a\u8fc7 Vue.use \u8c03\u7528\u7684\uff0c\u90a3 Vuex \u662f\u8981\u6ee1\u8db3\u8c03\u7528\u6761\u4ef6\u7684\uff0c\u770b\u4e0b\u6e90\u7801\uff08 2.4.0 \u7248\u672c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let Vue;\n\nclass Store{\n    ...\n}\n\nfunction install(_Vue){\n  \/\/ \u8fd9\u91cc\u4e5f\u662f\u9632\u6b62\u591a\u6b21\u8c03\u7528 Vue.use   \n  if (Vue) {\n    console.error(\n      '[vuex] already installed. Vue.use(Vuex) should be called only once.'\n    )\n    return\n  }\n  Vue = _Vue\n  applyMixin(Vue)\n}\n\nexport default {\n  Store,\n  install,\n  version: '__VERSION__',\n  mapState,\n  mapMutations,\n  mapGetters,\n  mapActions\n}<\/pre>\n\n\n\n<p>\u6e90\u7801\u4e2d\u770b\u51fa\u786e\u5b9e\u5305\u62ec install \u65b9\u6cd5\uff0c\u5b83\u91cc\u9762\u5f88\u7b80\u5355\u53ea\u662f\u8c03\u7528\u4e86 applyMixin \u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export default function (Vue) {\n  const version = Number(Vue.version.split('.')[0])\n\n  if (version >= 2) {\n    \/\/ Vue 2.x \u4f7f\u7528 mixin \u65b9\u6cd5 \u628a vuexInit \u6df7\u5165\u5230 beforeCreate \u94a9\u5b50\u4e2d  \n    Vue.mixin({ beforeCreate: vuexInit })\n  } else {\n    ...\n  }\n\n  function vuexInit () {\n     ...\n  }\n}<\/pre>\n\n\n\n<p>\u4ece\u4e0a\u770b\u51fa install \u65b9\u6cd5\u6839\u636e Vue \u7248\u672c\u4e0d\u540c\u628a vuexInit \u6df7\u5165\u5230\u7ec4\u4ef6\u4e2d\uff0cVuex \u7684\u4f7f\u7528\u662f\u628a\u5b9e\u4f8b\u5316\u7684 store \u4ee5\u9009\u9879\u7684\u5f62\u5f0f\u653e\u5230 Vue \u6839\u7ec4\u4ef6\u4e2d\uff0c\u90a3\u5b50\u7ec4\u4ef6\u662f\u5982\u4f55\u83b7\u5f97 store \u7684\u5462\uff1f\u90a3\u5c31\u8981\u4f9d\u9760 vuexInit \u4e86\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"monokai\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function vuexInit () {\n    const options = this.$options\n    \n    if (options.store) {\n      \/\/ \u6ce8\u5165\u5230\u6839\u7ec4\u4ef6  \n      this.$store = typeof options.store === 'function'\n        ? options.store()\n        : options.store\n    } else if (options.parent &amp;&amp; options.parent.$store) {\n      \/\/ \u5b50\u7ec4\u4ef6 $store \u5f15\u7528\u7236\u7ec4\u4ef6\u7684 $store \uff0c\u4fdd\u8bc1\u5168\u90e8\u4f7f\u7528\u7684\u662f\u540c\u4e00\u4e2a store \u5bf9\u8c61\u3002  \n      this.$store = options.parent.$store\n    }\n  }<\/pre>\n\n\n\n<p>\u4e3b\u8981\u533a\u5206\u4e86\u6839\u7ec4\u4ef6\u548c\u5b50\u7ec4\u4ef6\u662f\u5982\u4f55\u83b7\u53d6 store \u5bf9\u8c61\u7684\u3002<\/p>\n\n\n\n<p>\u81f3\u6b64 Vuex \u6ce8\u5165 Vue \u7ec4\u4ef6\u5c31\u8bb2\u89e3\u5b8c\u4e86\uff0c\u603b\u7ed3\uff1a\u5173\u952e\u70b9\u662f\u5229\u7528 mixin \u65b9\u6cd5\u6df7\u5165\u4e00\u4e2a\u65b9\u6cd5\uff0c\u7136\u540e\u65b9\u6cd5\u5185\u90e8\u6839\u636e options.parent \u6765\u4e00\u5c42\u5c42\u5f15\u7528\u7236\u7ea7 store \u5c5e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vuex \u662f\u4ec0\u4e48\uff1f Vuex is a&nbsp;state management pattern &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":576,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,3],"tags":[6,22,30],"class_list":["post-529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-frontend","category-tech","tag-javascript","tag-vuejs","tag-30"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=529"}],"version-history":[{"count":5,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions\/577"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/media\/576"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}