解决此问题的最简单方法是,只要任何可观察的属性发生变化,就会触发mobx“自动运行”。为此,您可以按照我对这个问题的回答。
我将在此处放置一些示例代码,以帮助您入门:
function autoSave(store, save) { let firstRun = true; mobx.autorun(() => { // This pre will run every time any observable property // on the store is updated. const json = JSON.stringify(mobx.toJS(store)); if (!firstRun) { save(json); } firstRun = false; });}class MyStore { @mobx.observable prop1 = 999; @mobx.observable prop2 = [100, 200]; constructor() { this.load(); autoSave(this, this.save.bind(this)); } load() { if () { const data = ; mobx.extendObservable(this, data); } } save(json) { // Now you can do whatever you want with `json`. // e.g. save it to session storage. alert(json); }}


