0

这是代码:

@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

4

1 回答 1

2

计算旨在(在概念上)纯粹。并且行动旨在(从概念上)不纯。因此,尽管从技术上讲它可能是一个很好的组合,但从概念上讲它们不是。

但不要害怕,只需检查 mobx-utils orcomputed-async-mobx` 包。它们可能包含您正在寻找的即用型抽象。

于 2016-12-27T14:46:56.337 回答