Vuex
2020-10-20 10:01:14 0 举报
AI智能生成
登录查看完整内容
源码Vuex
作者其他创作
大纲/内容
Vuex
Store
constructor (options = {})
this._modules = new ModuleCollection(options)
ModuleCollection
constructor (rawRootModule)
get (path)
getNamespace (path)
update (rawRootModule)
targetModule.update(newModule)
调用module.js中的update方法
if (newModule.modules),存在子模块,递归调用
Module
get namespaced ()
return !!this._rawModule.namespaced
this._children[key] = module
removeChild (key)
delete this._children[key]
getChild (key)
return this._children[key]
update (rawModule)
forEachChild (fn)
forEachGetter (fn)
forEachAction (fn)
forEachMutation (fn)
if (path.length === 0) { font color=\"#f15a23\
register nested modules
unregister (path)
const key = path[path.length - 1]
只可注销register的,不可注销构造函数创造的
if (!parent.getChild(key).runtime) return
parent.removeChild(key)
const state = this._modules.root.state
const namespace = store._modules.getNamespace(path)
if (module.namespaced)
store._modulesNamespaceMap[namespace] = module
set state
获取上下文
const noNamespace = namespace === ''
dispatch
noNamespace ?
是
store.dispatch
否
commit
noNamespace ?
store.commit
Object.defineProperties local
getters
判断缓存if (!store._makeLocalGettersCache[namespace])
const gettersProxy = {}
const splitPos = namespace.length
Object.keys(store.getters).forEach(type => {})
const localType = type.slice(splitPos)
store._makeLocalGettersCache[namespace] = gettersProxy
return store._makeLocalGettersCache[namespace]
state
return local
注册mutations
const entry = store._mutations[type] || (store._mutations[type] = [])
entry.push
注册actions
const entry = store._actions[type] || (store._actions[type] = [])
function wrappedActionHandler (payload)
if (!isPromise(res)) { res = Promise.resolve(res) }
return res
注册getters
遍历子模块,递归安装
const oldVm = store._vm // bind store public getters store.getters = {} // reset local getters cache store._makeLocalGettersCache = Object.create(null) const wrappedGetters = store._wrappedGetters const computed = {}
forEachValue wrappedGetters
enableStrictMode (store)
if oldVm
if hot
store._withCommit(() => { oldVm._data.$$state = null })
Vue.nextTick(() => oldVm.$destroy())
plugins.forEach(plugin => plugin(this))
get state ()
return this._vm._data.$$state
unifyObjectStyle
const entry = this._mutations[type]
this._withCommit(() => { entry.forEach(function commitIterator (handler) { handler(payload) }) })
const entry = this._actions[type]
this._actionSubscribers中before函数的执行
const result = entry.length > 1 ? Promise.all(entry.map(handler => handler(payload))) : entry[0](payload)
result.then中执行this._actionSubscribers中的after函数
subscribe (fn)
genericSubscribe
subscribeAction (fn)
replaceState (state)
replaceState (state) { this._withCommit(() => { this._vm._data.$$state = state }) }
if (typeof path === 'string') path = [path]
unregisterModule (path)
this._modules.unregister(path)
resetStore(this)
hotUpdate
install
applyMixin(vue)
Vue.mixin({ beforeCreate: vuexInit })
options.store
this.$store = options.store
options.parent && options.parent.$store
this.$store = options.parent.$store
mapState
normalizeNamespace
normalizeMap
res[key] = function mappedState
mapMutations
res[key] = function mappedMutation (...args)
mapGetters
res[key] = function mappedGetter
val = namespace + val
return this.$store.getters[val]
mapActions
res[key] = function mappedAction (...args)
createNameSpacedHelpers
0 条评论
回复 删除
下一页