在Service中添加一个reset方法,可以用来清空Service变量。 示例代码:
@Injectable({ providedIn: 'root' })
export class DataService { data: any[];
constructor() { }
reset() { this.data = []; } }
在需要清空data的地方,例如保存数据之后,可以调用reset方法:
this.dataService.saveData(this.data).subscribe( () => { this.dataService.reset(); } );