国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

詳解vuex之store源碼簡單解析

2024-05-06 15:38:04
字體:
來源:轉載
供稿:網友

關于vuex的基礎部分學習于http://www.survivalescaperooms.com/article/163008.htm

使用Vuex的時候,通常會實例化Store類,然后傳入一個對象,包括我們定義好的actions、getters、mutations、state等。store的構造函數:

export class Store { constructor (options = {}) {  // 若window內不存在vue,則重新定義Vue  if (!Vue && typeof window !== 'undefined' && window.Vue) {   install(window.Vue)  }  if (process.env.NODE_ENV !== 'production') {   // 斷言函數,來判斷是否滿足一些條件   // 確保 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.`)  }  // 解構賦值,拿到options里的plugins和strict  const {   plugins = [],   strict = false  } = options  // 創建內部屬性  // 標志一個提交狀態,作用是保證對 Vuex 中 state 的修改只能在 mutation 的回調函數中,而不能在外部隨意修改 state  this._committing = false   // 用來存儲用戶定義的所有的actions  this._actions = Object.create(null)  this._actionSubscribers = []  // 用來存儲用戶定義所有的mutatins  this._mutations = Object.create(null)  // 用來存儲用戶定義的所有getters   this._wrappedGetters = Object.create(null)  // 用來存儲所有的運行時的 modules  this._modules = new ModuleCollection(options)  this._modulesNamespaceMap = Object.create(null)  // 用來存儲所有對 mutation 變化的訂閱者  this._subscribers = []  // 一個 Vue對象的實例,主要是利用 Vue 實例方法 $watch 來觀測變化的  this._watcherVM = new Vue()  // 把Store類的dispatch和commit的方法的this指針指向當前store的實例上  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)  }  // 是否開啟嚴格模式  this.strict = strict  const state = this._modules.root.state  // Vuex的初始化的核心,其中,installModule方法是把我們通過options傳入的各種屬性模塊注冊和安裝;  // resetStoreVM 方法是初始化 store._vm,觀測 state 和 getters 的變化;最后是應用傳入的插件。  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本身是單一狀態樹,應用的所有狀態都包含在一個大對象內,隨著我們應用規模的不斷增長,這個Store變得非常臃腫。為了解決這個問題,Vuex允許我們把store分模塊。每一個模塊包含各自的state、mutations、actions和getters,甚至還可以嵌套模塊。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 乌兰县| 永平县| 镇原县| 营口市| 肥乡县| 通海县| 兴化市| 靖远县| 兰考县| 万全县| 阜阳市| 温泉县| 柏乡县| 东乡族自治县| 石景山区| 集贤县| 和田县| 六枝特区| 祁门县| 沧源| 万全县| 利川市| 延庆县| 长海县| 遂宁市| 双城市| 北碚区| 都兰县| 丰县| 云安县| 偃师市| 汕头市| 万盛区| 莒南县| 如皋市| 巴南区| 当雄县| 报价| 繁峙县| 绵阳市| 贵州省|