如果想自动保存 observable 数据的话我是这么做的
```
import { observable, action, autorun, toJS, set } from "mobx";
function autoSave(store, save) {
let firstRun = true;
autorun(() => {
// 此代码将在每次运行任何可观察属性时运行
// 对 store 进行更新。
const json = JSON.stringify(toJS(store));
if (!firstRun) {
save(json);
}
firstRun = false;
});
}
class RouteState {
@
observable state = {};
constructor() {
this.load();
autoSave(this, this.save.bind(this));
}
load() {
const storeTemp = sessionStorage.getItem("route_state");
if (storeTemp) {
const data = JSON.parse(storeTemp);
set(this, data);
}
}
save(json) {
sessionStorage.setItem("route_state", json);
}
@
action.bound
actionState(_state) {
this.state = _state;
}
}
```
[可以参考下这个博客](
https://www.cnblogs.com/beilo/p/10996385.html)
[参考链接 1](
https://stackoverflow.com/questions/40292677/how-to-save-mobx-state-in-sessionstorage)