vuex-module-decorators 动态注册 多个命名空间的模块 如何引用其他模块的方法

vuex-module-decorators 动态注册 多个命名空间的模块 如何引用其他模块的方法

// common 模块
@Module({ name: 'common', dynamic: true, namespaced: true, store, stateFactory: true })
export default class Common extends VuexModule {
     @Action({ commit: 'other/updateName' }) // other 是其他模块下面的Mutations
     useOtherMutations(data: UserInfoSchame):UserInfoSchame {
         console.log(this);
         return data;
    }
 
     @Action({ commit: 'rootFun' }) // rootFun是全局的 Mutation的方法
     useRootMutations(data: UserInfoSchame):UserInfoSchame {
         console.log(this);
         return data;
     }
}

// other模块
@Module({ name: 'other', dynamic: true, namespaced: true, store, stateFactory: true })
export default class Other extends VuexModule {
     @Mutation
     updateName(data: string): void{
        this.name = 'hello world';
     }
}

// 全局 mutations
export default {
 rootFun(state:State, data?: string): void{
    state.name = 'hello world';
 }
}

上面的commit写法如果按照传统的vuex写法 是没有问题的 但是vuex-module-decorators 写法就会有问题 如果把namespaced:false 就没问题 为 true就会报错:如下

vuex.esm.js?2f62:787 [vuex] unknown local mutation type: other/updateName, global type: common/other/updateName

阅读 2.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进