概述概述import Vuex from 'vuex'const store = new Vuex.Store({ ...options })构造器选项state类型: ObjectVuex store 实例的根 state 对象mutations类型: { [type: string]: Function }在 store 上注册 mutation,处理函数总是接受 state 作为第一个参数(如果定义在模块中,则为模块的局部状态),payload 作为第二个参数(可选)actions类型: { [type: string]: Function }在 store 上注册 action。处理函数接受一个 context 对象,包含以下属性:
{
state,
// 等同于 store.state, 若在模块中则为局部状态
rootState, // 等同于 store.state, 只存在于模块中
commit,
// 等同于 store.commit
dispatch, // 等同于 store.dispatch
getters
// 等同于 store.getters
}
{
state,
// 等同于 store.state, 若在模块中则为局部状态
rootState, // 等同于 store.state, 只存在于模块中
commit,
// 等同于 store.commit
dispatch, // 等同于 store.dispatch
getters
// 等同于 store.getters
}getters类型: { [key: string]: Function }在 store 上注册 getter,getter 方法接受以下参数: state,
// 如果在模块中定义则为模块的局部状态 getters,
// 等同于 store.getters当定义在一个模块里时会特别一些
state,
// 如果在模块中定义则为模块的局部状态
getters,
// 等同于 store.getters
rootState
// 等同于 store.state
rootGetters
// 所有 getters注册的 getter 暴露为 store.gettersmodules类型: Object包含了子模块的对象,会被合并到 store
{
key: {

state,

namespaced?,

mutations,

actions?,

getters?,

modules?
},
...
}
{
key: {

state,

namespaced?,

mutations,

actions?,

getters?,

modules?
},
...
}与根模块的选项一样,每个模块也包含 state 和 mutations 选项。模块的状态使用 key 关联到 store 的根状态。模块的 mutation 和 getter 只会接收 module 的局部状态作为第一个参数,而不是根状态,并且模块 action 的 context.state 同样指向局部状态plugins类型: Array一个数组,包含应用在 store 上的插件方法。这些插件直接接收 store 作为唯一参数,可以监听 mutation(用于外部地数据持久化、记录或调试)或者提交 mutation (用于内部数据,例如 websocket 或 某些观察者)strict类型: Boolean默认值: false使 Vuex store 进入严格模式,在严格模式下,任何 mutation 处理函数以外修改 Vuex state 都会抛出错误实例属性state类型: Object根状态,只读getters类型: Object暴露出注册的 getter,只读 实例方法
commit(type: string, payload?: any, options?: Object) | commit(mutation: Object, options?: Object)
commit(type: string, payload?: any, options?: Object) | commit(mutation: Object, options?: Object)提交 mutation。options 里可以有 root: true,它允许在命名空间模块里提交根的 mutation
dispatch(type: string, payload?: any, options?: Object) | dispatch(action: Object, options?: Object)
dispatch(type: string, payload?: any, options?: Object) | dispatch(action: Object, options?: Object)分发 action。options 里可以有 root: true,它允许在命名空间模块里分发根的 action。返回一个解析所有被触发的 action 处理器的 Promise
replaceState(state: Object)
replaceState(state: Object)替换 store 的根状态,仅用状态合并或时光旅行调试
watch(getter: Function, cb: Function, options?: Object)
watch(getter: Function, cb: Function, options?: Object)响应式地监测一个 getter 方法的返回值,当值改变时调用回调函数。getter 接收 store 的状态作为唯一参数。接收一个可选的对象参数表示 Vue 的 vm.$watch 方法的参数。要停止监测,直接调用返回的处理函数
subscribe(handler: Function)
subscribe(handler: Function)注册监听 store 的 mutation。handler 会在每个 mutation 完成后调用,接收 mutation 和经过 mutation 后的状态作为参数
store.subscribe((mutation, state) => {
console.log(mutation.type)
console.log(mutation.payload)
})
store.subscribe((mutation, state) => {
console.log(mutation.type)
console.log(mutation.payload)
})通常用于插件
registerModule(path: string | Array, module: Module)
registerModule(path: string | Array, module: Module)注册一个动态模块
unregisterModule(path: string | Array)
unregisterModule(path: string | Array)卸载一个动态模块
hotUpdate(newOptions: Object)
hotUpdate(newOptions: Object)热替换新的 action 和 mutation辅助函数
mapState(namespace?: string, map: Array | Object): Object
mapState(namespace?: string, map: Array | Object): Object为组件创建计算属性以返回 Vuex store 中的状态。第一个参数是可选的,可以是一个命名空间字符串
mapGetters(namespace?: string, map: Array | Object): Object
mapGetters(namespace?: string, map: Array | Object): Object为组件创建计算属性以返回 getter 的返回值。第一个参数是可选的,可以是一个命名空间字符串
mapActions(namespace?: string, map: Array | Object): Object
mapActions(namespace?: string, map: Array | Object): Object创建组件方法分发 action。第一个参数是可选的,可以是一个命名空间字符串
mapMutations(namespace?: string, map: Array | Object): Object
mapMutations(namespace?: string, map: Array | Object): Object创建组件方法提交 mutation。第一个参数是可选的,可以是一个命名空间字符串
createNamespacedHelpers(namespace: string): Object
createNamespacedHelpers(namespace: string): Object关于VUEX方面的相关知识请点击下面的相关链接