我有一个服务,该服务发送get请求以从mongodb集合中获取数据,并且我也希望将传入数据作为数组存储到localstorage中。
API.SERVICE.TS
getStorageLocationsList(){
this.setHeader();
return this.http.get(this.localURL + 'storagelocation/view' + '?userplant=' + this.userplant , {headers:this.appHeader});
}
COMPONENT.TS
this._api.getStorageLocationsList().subscribe(res => {
this.storagelocationsList = res as Event[];
//console.log(res);
}, err => {
console.log(err);
});
//I want some more code here to store the data in res to the localstorage
答案 0 :(得分:2)
this._api.getStorageLocationsList().subscribe(res => {
this.storagelocationsList = res as Event[];
//console.log(res);
let storagelocationsList = ['A', 'B']; // suppose this is your api response
localStorage.setItem('locationList', JSON.stringify(storagelocationsList));
}, err => {
console.log(err);
});
要获取:
JSON.parse(localStorage.getItem('locationList'));
有关localStorage的更多操作,请检查this