gpt4 book ai didi

typescript - 如何在 vuex-module-decorators 中使用 MutationAction?

转载 作者:行者123 更新时间:2023-12-04 00:27:00 25 4
gpt4 key购买 nike

我正在尝试使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com