vue.js - 在 vuex-module-decorators 中创建可重用的 getter
问题描述
使用 vuex-module-decorators 遇到这样的问题。我想创建一些父模块,以便子模块可以从中扩展并继承它的动作和吸气剂。但是吸气剂不是继承的。
我是这样尝试的:
我的parent-module.ts
:
import {Action, getModule, Module, Mutation, VuexModule} from 'vuex-module-decorators';
export class ParentStore extends VuexModule {
public get getterForInherit(): any {
return someData
}
}
子模块
child-one.ts
:
import {Action, getModule, Module, Mutation, VuexModule} from 'vuex-module-decorators'
import {ParentModule} from './parent-module';
@Module({dynamic: true, store: Store, name: 'childOne', namespaced: true})
class FirstChildModule extends ParentModule {
public get SecondChildGetter(): number {
return 1;
}
}
export const FirstChildStore: ParentModule = getModule(FirstChildModule)
child-two.ts
:
import {Action, getModule, Module, Mutation, VuexModule} from 'vuex-module-decorators'
import {ParentModule} from './parent-module';
@Module({dynamic: true, store: Store, name: 'childTwo', namespaced: true})
class SecondChildModule extends ParentModule {
public get FirstChildGetter(): number {
return 2;
}
}
export const SecondChildStore: ParentModule = getModule(SecondChildModule)
但是当我将这些模块导入组件时getterForInherit
不可用。有可能这样做吗?
解决方案
我认为与其尝试使用不同的包来处理您的突变、动作和吸气剂;根据您的问题,我假设您希望能够从父组件或子组件访问您的 getter 和操作。如果您已经安装了 vuex,则可以使用它。
你可以这样做:
import { mapGetters, mapActions, mapMutations } from 'vuex'
methods: {
...mapActions(['submitTransaction', 'submitNotification']),
...mapMutations(['clearNotificationData']),
},
computed: {
...mapGetters([
'messageData',
'isMessageLoaded',
'isProcessingRequest',
]),
推荐阅读
- macos - 以编程方式实现 Mac 长按重音弹出
- java - 通过组合框添加外键时摆动应用程序出错
- android - Android Studio 中的图像混合模式(如 photoshop、paint.net)?
- javascript - 如何在密码中显示前四个字符并在其旁边显示星号?
- git - yarn install 在 git post-receive 钩子中克隆 github 包时失败
- c++ - Project Euler #8 C++ 得到不正确的答案
- python - 如何在python上用pywinauto打开一个excel文件?
- javascript - 导入“sjcl”时 Webpack 4 不需要的库。
- java - 为什么函数在循环中被多次调用?
- python - 如何为自定义数据集设置 Retinanet?