Vuex之理解Store的用法
2017-04-19 14:53
791 查看
1.什么是Store?
上一篇文章说了,
Vuex就是提供一个仓库,
Store仓库里面放了很多对象。其中state就是数据源存放地,对应于与一般
Vue对象里面的
data(后面讲到的
actions和
mutations对应于
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)}}
后面文章逐步分析每一个模块。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章推荐
- Vuex之理解state的用法实例
- 关于C的关键字——const的理解和用法小总结
- 關於 和 用法的個人理解...
- Thread.Join()用法的理解
- C++中this指针的理解和用法
- C的关键字——const的理解和用法
- static 在C的用法理解
- 理解JTable的用法
- 完成端口的深入理解及一种变态用法
- const的理解和用法
- 關於 和 用法的個人理解...
- 理解STL中的函数子,函数子类,和其用法
- fork()函数用法理解
- java中assert的用法理解
- 完成端口的深入理解及一种变态用法
- Eval()的用法,有点不是很理解
- static和this的理解和用法小节(转载)
- 关于C的关键字——const的理解和用法
- C的关键字——const的理解和用法
- Struts新用法以及关于理解