# /src/index.js 文件
import {
Store, install } from './store'
import {
mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from './helpers'
import createLogger from './plugins/logger'
export default {
Store,install,version: '__VERSION__',mapState,mapMutations,mapGetters,mapActions,createNamespacedHelpers,createLogger
}
export {
Store,install,mapState,mapMutations,mapGetters,mapActions,createNamespacedHelpers,createLogger
}
createNamespacedHelpers 的使用
import {
createNamespacedHelper } from "vuex";
const {
mapState, mapGetters, mapMutations, mapActions } = createNamespaceHelpers("namespaceValue");
const {
message, ...otherState } = mapState(["message", "otherState1", "otherState2"]);
const {
updateMessage, ...otherMutations } = mapMutations(["updateMessage", "otherMutation1", "otherMutations2"]) 当然,对于Store中的子模块读取属性,还有如下方式:
import {
mapState, mapGetters } from 'vuex'
computed: {
...mapState("publish", ["fileObj", "nowUploadType"]),...mapGetters("publish", ["getFileObjMax", "fileList"]),
}
createLogger 的使用
- 引入日志插件后,每次操作state中的值,都会打印数据状态,并且会区别是actions还是mutations。如果是子模块会在方法名前加上子模块名。
- strict: true 设置严格模式后,actions中不能直接通过 state.name = val 去修改state中的值,而要通过commit去触发mutations中的方法修改state,否则报错。
import {
createLogger } from 'vuex';
const store = new Vuex.store({
state: {
number: 100, name: '小明' },mutations: {
setNumber( state, val ){
state.number++;},changeName( state, val ){
state.name = val;}},actions: {
updateNumber( {
commit, state }, val ){
state.number++;}},plugins: process.env.NODE_DEV !== 'production' ? [createLogger()]:[],strict: true
});export default store;