我怎么能用mobx和axios写这个?我正在尝试使用箭头功能来保持"这个"但我可能做错了。
@action saveMode() {
axios.post('/Course/Post', { Name: "test41515"})
.then(response => {
console.log(response, this.isEditing);
this.isEditing = !this.isEditing;
this.failedValidation = [];
})
}
Uncaught (in promise) Error: [mobx] Invariant failed: Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: Course@5.isEditing
at invariant (app.bundle.js:7316)
at fail (app.bundle.js:7311)
at checkIfStateModificationsAreAllowed (app.bundle.js:7880)
at ObservableValue.prepareNewValue (app.bundle.js:5786)
at setPropertyValue (app.bundle.js:6663)
at Course.set [as isEditing] (app.bundle.js:6631)
at app.bundle.js:62336
at <anonymous>
答案 0 :(得分:0)
MobX在其文档中有关于编写异步操作的整页。这将是一个开始的地方,https://mobx.js.org/best/actions.html,事实上,第一个例子正是你所面对的问题
上面的示例将抛出异常,因为传递给fethGithubProjectsSomehow promise的回调不是fetchProjects操作的一部分,因为操作仅适用于当前堆栈。
根据您的代码段,最简单的解决方法是使用?scan
runInAction