我正在尝试使用 https://championswimmer.in/vuex-module-decorators 中的 vuex-module-decorators .
假设我的模块状态有一个 Prop dir
这是一个字典:{[key: string]: string}
我可以使用 @MutationAction
更新该字典的元素?下面的代码不正确,但希望能理解我的意思。
export default class Module extends VuexModule {
dir: {[key: string]: string}
@MutationAction(???)
updateDir(keyValue) {
return keyValue // ???
}
}
是否有一些关于如何使用的文档
@MutationAction
,它需要什么参数以及它提交的实际突变是什么?
最佳答案
这是你应该做的 -
export default class Module extends VuexModule {
dir: {[key: string]: string}
@MutationAction(['dir'])
async updateDir(keyValue) {
const currDir = this.dir
// do any async work (network req) here
currDir['myKey'] = keyValue
return ( { dir: currDir } )
}
}
但是,如果您真的不需要任何异步工作。
export default class Module extends VuexModule {
dir: {[key: string]: string}
@Mutation
async updateDir(keyValue) {
this.dir.myKey = keyValue
}
}
附言我是
vuex-module-decorators
的作者
关于typescript - 如何在 vuex-module-decorators 中使用 MutationAction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55730545/