[英]Typescript Vuex - How to define a state with a setter and getter?
In the vuex store I want to initialize a state called _token. 在vuex存储中,我想初始化一个称为_token的状态。 When I'm trying to access the property within the same class, it gives me the error message that the setter for _token is not defined.
当我尝试访问同一类中的属性时,它给我以下错误消息:_token的设置器未定义。 Anyone has an idea why only the getter is available?
有人知道为什么只有吸气剂可用吗?
export default class Api extends VuexModule {
public _token = '';
@Action
public async [VuexActionsApi.VerifyUser](data: any): Promise<IResponseState> {
const vuexInstance = this;
// this is not working
vuexInstance._token = "test";
}
If you are going to use class decorators then you must import them (and install the package) like this: 如果要使用类装饰器,则必须按如下所示导入它们(并安装程序包):
import Action from 'vue-class-component'
Although I might advise against that syntax as it is being deprecated in vue3, see here: https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121 尽管由于vue3中已弃用该语法,我可能会建议不要这样做,但请参见此处: https : //github.com/vuejs/rfcs/pull/17#issuecomment-494242121
It seems to work now, when i defined the state like this: 当我这样定义状态时,它现在似乎可以工作:
public data = {
bearerToken: undefined
};
Now the setter and getter functions are created correctly. 现在,正确地创建了setter和getter函数。 Someone has an idea why the first approach didn't work?
有人知道为什么第一种方法不起作用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.