我有一个对象:
export class ObjectLayer {
id: number;
name: string;
}
我从服务器得到这个并设置为currentVersion
:
public currentVersion: ObjectLayer;
setVersion(object: ObjectLayer) {
this.currentVersion = object;
}
然后用户可以更改currentVersion
版本。如何随时将版本恢复到初始版本?
保存初始状态。
private _oldVersion?: ObjectLayer;
public currentVersion: ObjectLayer;
setVersion(object: ObjectLayer) {
this._oldVersion = this.currentVersion;
this.currentVersion = object;
}
restoreVersion() {
if (!this._oldVersion) throw new Error('Nothing to restore');
this.currentVersion = this._oldVersion;
this._oldVersion = undefined;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句