?
This document uses PHP Chinese website manual Release
import Vuex from 'vuex' const store = new Vuex.Store({ ...options })
state
類型: Object
Vuex store 的根state對象。
Details
mutations
類型: { [type: string]: Function }
在 store 上注冊 mutation,Handler function總是接收 state 作為第一個參數(shù)(如果定義在模塊中,則作為模塊的本地狀態(tài)),如果有則接收 payload 作為第二個參數(shù)。
actions
類型: { [type: string]: Function }
在 store 上注冊 action。Handler function接收一個 context 對象,這個對象提供以下屬性:
{ state, // 等同于 store.state,如果在模塊中則是本地狀態(tài) rootState, // 等同于 store.state,模塊中才有 commit, // 等同于 store.commit dispatch, // 等同于 store.dispatch getters // 等同于 store.getters }
getters
類型: { [key: string]: Function }
在 store 上注冊 getter,getter 方法接收一下參數(shù):
注冊的 getter 被暴露在 store.getters。
state, // 如果在模塊中定義則為模塊的本地狀態(tài) getters, // 等同于 store.getters rootState // 等同于 store.state
modules
類型: Object
包含了子模塊的對象,會被合并到 store,形如:
每個模塊可以包含 state 和 mutations,就像根模塊的配置。一個模塊的狀態(tài)使用模塊的 key 附加到 store 的根狀態(tài)上。模塊的 mutation 和 getter 將接收 module 的本地狀態(tài)作為第一個參數(shù),而不是根狀態(tài),并且模塊 action 的 context.state 也指向本地狀態(tài)。
{ key: { state, mutations, actions?, getters?, modules? }, ... }
plugins
類型: Array<Function>
一個插件方法的數(shù)組被應(yīng)用于 store 上。這些插件直接接收 store 作為唯一參數(shù),也可以監(jiān)聽 mutation(用于外部地數(shù)據(jù)持久化、記錄或調(diào)試)或者提交 mutation (用于內(nèi)部數(shù)據(jù),例如 websocket 或 某些觀察者)。
strict
類型: Boolean
默認值: false
強制 Vuex store 進入嚴格模式,在嚴格模式下,在 mutation 處理器以外修改 Vuex state 則會拋出錯誤。
state
類型: Object
The root state. Read only.
getters
類型: Object
暴露注冊的 getter,只讀。
commit(類型: string, payload?: any) | commit(mutation: Object)
提交一個 mutation.
dispatch(type: string, payload?: any) | dispatch(action: Object)
分發(fā)一個 action。返回 action 方法的返回值,如果多個處理器被觸發(fā),那么返回一個 Pormise。
replaceState(state: Object)
替換 store 的根狀態(tài),僅用于調(diào)試 state。
watch(getter: Function, cb: Function, options?: Object)
響應(yīng)式的監(jiān)測一個 getter 方法的返回值,當(dāng)值改變時調(diào)用回調(diào)。getter 接收 store 的 state 作為唯一參數(shù)。接收類似 vm.$watch 方法的參數(shù)的可選對象。
要停止檢測,調(diào)用該方法的返回值 (watch 的返回值是個方法)。
subscribe(handler: Function)
訂閱(注冊監(jiān)聽) store 的 mutation。handler 會再每個 mutation 完成后調(diào)用,接收 mutation 的描述對象和 經(jīng)過 mutation 后的狀態(tài)作為參數(shù):
這是插件中常見的用法。
store.subscribe((mutation, state) => { console.log(mutation.type) console.log(mutation.payload) })
registerModule(path: string | Array<string>, module: Module)
注冊一個動態(tài)模塊。
unregisterModule(path: string | Array<string>)
注銷一個動態(tài)模塊。
hotUpdate(newOptions: Object)
熱加載新的 action 和 mutation。
mapState(map: Array<string> | Object): Object
創(chuàng)建一個組件的計算屬性選項,該選項會返回 Vuex store 的子樹。
mapGetters(map: Array<string> | Object): Object
創(chuàng)建一個組件的計算屬性選項,該選項會返回 getter的計算后的值。
mapActions(map: Array<string> | Object): Object
創(chuàng)建一個組件的methods選項,該選項返回一個 action。
mapMutations(map: Array<string> | Object): Object
創(chuàng)建一個組件的methods選項,該選項提交一個 mutation。