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

首頁 > 編程 > JavaScript > 正文

Vuex之理解Store的用法

2019-11-19 16:47:32
字體:
來源:轉載
供稿:網友

1.什么是Store?

上一篇文章說了,Vuex就是提供一個倉庫,Store倉庫里面放了很多對象。其中state就是數據源存放地,對應于與一般Vue對象里面的data(后面講到的actionsmutations對應于methods)。

在使用Vuex的時候通常會創建Store實例new Vuex.store({state,getters,mutations,actions})有很多子模塊的時候還會使用到modules

總結,Store類就是存儲數據和管理數據方法的倉庫,實現方式是將數據和方法已對象形式傳入其實例中。要注意一個應用或是項目中只能存在一個Store實例!!

2.Store源碼分析

class Store{  constructor (options = {}) {  // 1.部分2個‘斷言函數'判斷條件  assert(Vue, `must call Vue.use(Vuex) before creating a store   instance.`) // 在Store實例化之前一定要確保Vue的存在  assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)  //確保promise存在    // 2.結構賦值拿到options里面的state,plugins和strict  const {  state = {}, //rootState  plugins = [], // 插件  strict = false //是否嚴格模式   } = options     // 3.Store internal state創建store內部屬性  this._options = options //存儲參數  this._committing = false //標識提交狀態,保證修改state只能在mutation里面,不能在外部隨意修改  this._actions = Object.create(null) //存儲用戶定義的actions  this._mutations = Object.create(null) //存儲用戶定義的mutations  this._wrappedGetters = Object.create(null) //存儲用戶定義的getters  this._runtimeModules = Object.create(null) //存儲運行時的modules  this._subscribers = [] //存儲所有堵mutation變化的訂閱者  this._watcherVM = new Vue() //借用Vue實例的方法,$watch來觀測變化    // 4.將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)}}

后面文章逐步分析每一個模塊。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 前郭尔| 肃宁县| 三门县| 东乡族自治县| 天等县| 博爱县| 高淳县| 明水县| 阳城县| 绍兴县| 霍林郭勒市| 开远市| 宁强县| 会同县| 长岭县| 上饶市| 库伦旗| 全南县| 沙湾县| 宝清县| 凤阳县| 夏津县| 电白县| 敦化市| 遂溪县| 普宁市| 台北县| 太白县| 乐山市| 南投县| 汉源县| 尚志市| 申扎县| 湖北省| 通渭县| 河西区| 蓬莱市| 应用必备| 铜鼓县| 红桥区| 江达县|