Typescript 枚举值作为类函数名
以前使用Vuex时,我可以使用枚举值作为我的操作和突变的函数名称,如下所示:Typescript 枚举值作为类函数名,typescript,webpack,enums,vuex,Typescript,Webpack,Enums,Vuex,以前使用Vuex时,我可以使用枚举值作为我的操作和突变的函数名称,如下所示: // ./store/modules/test/actions.ts import { Actions } from '@/store'; import { ActionTree } from 'vuex'; export default ActionTree<any,any>{ [Actions.TEST_ACTION](){ console.log('in action'); } }
// ./store/modules/test/actions.ts
import { Actions } from '@/store';
import { ActionTree } from 'vuex';
export default ActionTree<any,any>{
[Actions.TEST_ACTION](){
console.log('in action');
}
}
“我的编辑器”(vscode)中没有错误,没有任何东西试图调用此函数(没有调用dispatch),并且正在导出操作
枚举:
export enum Actions {
TEST_ACTION = 'testAction'
}
如果我改为执行以下操作,我将不再收到错误:
Cannot read property 'TEST_ACTION' of undefined
at eval (test.ts?f2af:4)
// Same line as 'export default class Test extends VuexModule'
@Action
public async testAction(): Promise<void> {
console.log('in action');
}
@操作
公共异步testAction():承诺{
console.log('in action');
}
更新:
如果枚举位于同一文件中,则不存在错误:
///存储/模块/测试.ts
从“vuex模块装饰器”导入{Action,Module,VuexModule};
导出枚举操作{
测试动作='testAction'
}
@模块
导出默认类测试扩展VuexModule{
@行动
公共异步[Actions.TEST\u ACTION]():承诺{
console.log('in action');
}
}
虽然这会奏效,但并不理想。这可能是网页包问题吗?听起来像是您试图在初始化类之前访问该类上的函数TEST\u操作。在发生错误的地方没有调度任何操作。问题源于“从@/store导入存储”行中的
main.ts
。这听起来像是导入了错误的操作