内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

vuex之store的示例分析

這篇文章將為大家詳細(xì)講解有關(guān)vuex之store的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)主營(yíng)薌城網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā)公司,薌城h5微信小程序搭建,薌城網(wǎng)站營(yíng)銷(xiāo)推廣歡迎薌城等地區(qū)企業(yè)咨詢(xún)

使用Vuex的時(shí)候,通常會(huì)實(shí)例化Store類(lèi),然后傳入一個(gè)對(duì)象,包括我們定義好的actions、getters、mutations、state等。store的構(gòu)造函數(shù):

export class Store {
 constructor (options = {}) {
  // 若window內(nèi)不存在vue,則重新定義Vue
  if (!Vue && typeof window !== 'undefined' && window.Vue) {
   install(window.Vue)
  }

  if (process.env.NODE_ENV !== 'production') {
   // 斷言函數(shù),來(lái)判斷是否滿(mǎn)足一些條件
   // 確保 Vue 的存在
   assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`)
   // 確保 Promsie 可以使用
   assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)
   assert(this instanceof Store, `store must be called with the new operator.`)
  }

  // 解構(gòu)賦值,拿到options里的plugins和strict
  const {
   plugins = [],
   strict = false
  } = options

  // 創(chuàng)建內(nèi)部屬性
  // 標(biāo)志一個(gè)提交狀態(tài),作用是保證對(duì) Vuex 中 state 的修改只能在 mutation 的回調(diào)函數(shù)中,而不能在外部隨意修改 state
  this._committing = false 
  // 用來(lái)存儲(chǔ)用戶(hù)定義的所有的actions
  this._actions = Object.create(null)
  this._actionSubscribers = []
  // 用來(lái)存儲(chǔ)用戶(hù)定義所有的mutatins
  this._mutations = Object.create(null)
  // 用來(lái)存儲(chǔ)用戶(hù)定義的所有g(shù)etters 
  this._wrappedGetters = Object.create(null)
  // 用來(lái)存儲(chǔ)所有的運(yùn)行時(shí)的 modules
  this._modules = new ModuleCollection(options)
  this._modulesNamespaceMap = Object.create(null)
  // 用來(lái)存儲(chǔ)所有對(duì) mutation 變化的訂閱者
  this._subscribers = []
  // 一個(gè) Vue對(duì)象的實(shí)例,主要是利用 Vue 實(shí)例方法 $watch 來(lái)觀測(cè)變化的
  this._watcherVM = new Vue()

  // 把Store類(lèi)的dispatch和commit的方法的this指針指向當(dāng)前store的實(shí)例上
  const store = this
  const { dispatch, commit } = this
  this.dispatch = function boundDispatch (type, payload) {
   return dispatch.call(store, type, payload)
  }
  this.commit = function boundCommit (type, payload, options) {
   return commit.call(store, type, payload, options)
  }

  // 是否開(kāi)啟嚴(yán)格模式
  this.strict = strict

  const state = this._modules.root.state

  // Vuex的初始化的核心,其中,installModule方法是把我們通過(guò)options傳入的各種屬性模塊注冊(cè)和安裝;
  // resetStoreVM 方法是初始化 store._vm,觀測(cè) state 和 getters 的變化;最后是應(yīng)用傳入的插件。
  installModule(this, state, [], this._modules.root)

  resetStoreVM(this, state)
  plugins.forEach(plugin => plugin(this))

  const useDevtools = options.devtools !== undefined ? options.devtools : Vue.config.devtools
  if (useDevtools) {
   devtoolPlugin(this)
  }
 }

Vuex本身是單一狀態(tài)樹(shù),應(yīng)用的所有狀態(tài)都包含在一個(gè)大對(duì)象內(nèi),隨著我們應(yīng)用規(guī)模的不斷增長(zhǎng),這個(gè)Store變得非常臃腫。為了解決這個(gè)問(wèn)題,Vuex允許我們把store分模塊。每一個(gè)模塊包含各自的state、mutations、actions和getters,甚至還可以嵌套模塊。

接下來(lái)看installModule方法:

function installModule (store, rootState, path, module, hot) {
 // 通過(guò)path數(shù)組的長(zhǎng)度判斷是否為根
 const isRoot = !path.length
 const namespace = store._modules.getNamespace(path)

 // register in namespace map
 if (module.namespaced) {
  store._modulesNamespaceMap[namespace] = module
 }

 // 第一次調(diào)用時(shí),path為空,不進(jìn)入if
 // 遞歸調(diào)用installModule安裝子模塊時(shí),將執(zhí)行該代碼塊
 if (!isRoot && !hot) {
  const parentState = getNestedState(rootState, path.slice(0, -1))
  // 模塊名
  const moduleName = path[path.length - 1]
  // 把當(dāng)前模塊的state添加到parentState中。具體解析見(jiàn)下
  store._withCommit(() => {
   Vue.set(parentState, moduleName, module.state)
  })
 }

 const local = module.context = makeLocalContext(store, namespace, path)

 // 對(duì)mutations、actions、getters進(jìn)行注冊(cè)
 module.forEachMutation((mutation, key) => {
  const namespacedType = namespace + key
  registerMutation(store, namespacedType, mutation, local)
 })

 module.forEachAction((action, key) => {
  const type = action.root ? key : namespace + key
  const handler = action.handler || action
  registerAction(store, type, handler, local)
 })

 module.forEachGetter((getter, key) => {
  const namespacedType = namespace + key
  registerGetter(store, namespacedType, getter, local)
 })

 // 遍歷modules,遞歸調(diào)用installModule安裝子模塊
 module.forEachChild((child, key) => {
  installModule(store, rootState, path.concat(key), child, hot)
 })
}
store的_withCommit方法定義是這樣的:

 _withCommit (fn) {
  const committing = this._committing
  this._committing = true
  fn()
  this._committing = committing
 }

Vuex中所有對(duì)state的修改都會(huì)用_withCommit函數(shù)包裝,保證在同步修改state的過(guò)程中this._committing的值始終為true。這樣當(dāng)我們觀測(cè) state的變化時(shí),如果this._committing的值不為true,則能檢查到這個(gè)狀態(tài)修改是有問(wèn)題的。

關(guān)于“vuex之store的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

本文題目:vuex之store的示例分析
文章鏈接:http://www.rwnh.cn/article30/ipcsso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、網(wǎng)站維護(hù)、外貿(mào)網(wǎng)站建設(shè)、用戶(hù)體驗(yàn)網(wǎng)站改版、動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站
延川县| 丰镇市| 乌兰县| 雷山县| 延寿县| 通渭县| 灌南县| 荔波县| 玉田县| 万州区| 乐东| 桦甸市| 乌拉特后旗| 翁牛特旗| 柳江县| 崇明县| 康马县| 广丰县| 洛阳市| 象州县| 昂仁县| 聂荣县| 泾阳县| 永清县| 襄城县| 巴林左旗| 大足县| 昌黎县| 遂宁市| 东乌| 日土县| 西昌市| 安国市| 洛川县| 柳州市| 东兰县| 静乐县| 西乡县| 邻水| 囊谦县| 喀什市|