typescript - 如何在 vuex-module-decorators 中使用 MutationAction?
问题描述
我正在尝试使用来自https://championswimmer.in/vuex-module-decorators的 vuex-module-decorators 。假设我的模块的 state 有dir
一个 dict 的 prop:{[key: string]: string}
我可以用它@MutationAction
来更新那个 dict 的元素吗?下面的代码不正确,但希望能理解我的意思。
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
}
}
PS我是作者vuex-module-decorators
推荐阅读
- r - 将 PDF 参数的 MLE 写为项的无限总和
- python - Python 类型提示 - 提示返回类的类型而不是给定类的实例
- terraform - 如何在没有整个突触工作区的情况下在 Terraform 中突触池/DW
- assembly - 汇编语言如何正确使用 WriteBinB?
- r - 最大化因变量总和的预测模型
- python - 如何将文本从 shell 转换为 html?
- multithreading - 两个线程,如何实现它们之间的通信。(伪代码)
- c - inet_ntop() 不打印网址
- c++ - 尝试在 C++ 中使用 getline (string) 来解析 kml 文件
- python - 如何使用 python 在当前光标位置打印文本?