验证中...
Vuex之集成
Raw Copy
步骤1. npm i vuex -S
步骤2.新建 store 目录 --> 新建 store.js:
// store.js
import Vuex from 'vuex'
export default () => {
return new Vuex.Store({
state: {
},
actions: {
},
mutations: {
},
getters: {
},
modules: {
// ...忽略模块
}
})
}
------------------------------------------------------------------
优化后为:
新建 store 目录
|
| state 目录
| | state.js // 全局state
|
| actions 目录
| | actions.js // 全局 actions
|
| mutations 目录
| | mutations.js // 全局 mutations
|
| getters 目录
| | getters.js // 全局 getters
|
| modules 目录
| | a.js // 模块a
| | b.js // 模块b
|
| store.js
//store.js
import Vuex from 'vuex'
import defaultState from './state/state'
import actions from './actions/actions'
import mutations from './mutations/mutations'
import getters from './getters/getters'
import a from './modules/a'
import b from './modules/b'
export default () => {
return new Vuex.Store({
state: defaultState,
actions,
mutations,
getters,
modules: {
a,
b
}
})
}
步骤3. 在项目入口文件中:
import Vue from 'vue'
import Vuex from 'vuex'
import createStore from './store/store'
Vue.use(Vuex)
const store = createStore()
new Vue({
//...忽略其它选项
store,
//...忽略其它选项
})

Comment list( 0 )

You need to Sign in for post a comment

Help Search