这是代码:
@computed
get user() {
if(!this.hasValidated)
this.reloadUserData();
return this.userData;
}
@action
reloadUserData() {
return new Promise(function(ok, err) {
if(!window.localStorage['atoken'])
err({id:24, detail:'User havn\'t logged in.'});
if(!window.localStorage['aprofile'])
apicall.get('user/detail').then((data)=>{
this.setProfile(data.data.content);
ok(true);
}).catch((derr)=>{
err({id:20, detail:derr});
});
else{
this.userData=JSON.parse(window.localStorage['aprofile']);
}
}.bind(this));
}
所以,主要目标是,当配置文件数据尚未验证时,我们将从服务器重新获取它,然后,在等待数据更改时,我们将从本地存储中为它们提供缓存的值。
Anddd ....我的问题是,为什么它会给我一个“计算值无法调用操作函数”的信息?
谢谢! :D
最佳答案
计算旨在(概念上)纯粹。并且 Actions 的目的是(概念上)不纯粹。因此,尽管从技术上讲它可能是一个很好的组合,但从概念上讲它们却不是。
但不用担心,只需检查 mobx-utils 或
compulated-async-mobx` 包即可。它们可能包含您正在寻找的现成可用的抽象。
关于javascript - Mobx 对计算属性调用操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41335128/