>服务层 ,storage.service.ts ~~~ public set(k:string,v:any){ localStorage.setItem(k,JSON.stringify(v)); } public getByKey(k:string):[]{ return JSON.parse(localStorage.getItem(k)); } ~~~ >component.ts ~~~ public history:Array<string>=[]; public keywords:string; ngOnInit(): void { //刷新页面 console.log("刷新"); let historyList=this.substance.getByKey("history"); if(historyList){ this.history=historyList; } } private saveHistory():void { if(this.history.indexOf(this.keywords)!=-1){ alert(this.keywords+"已经存在"); return; } this.history.push(this.keywords); this.substance.set('history',this.history); this.keywords=''; } delete(index):void{ this.history.splice(index,1); this.substance.set('history',this.history); } ~~~