angular - 如何通过get请求将mongodb的数据作为数组存储在本地存储中
问题描述
我有一个服务,它发送一个 get 请求来从我的 mongodb 集合中获取数据,我也想将传入的数据作为一个数组存储到 localstorage 中。
API.SERVICE.TS
getStorageLocationsList(){
this.setHeader();
return this.http.get(this.localURL + 'storagelocation/view' + '?userplant=' + this.userplant , {headers:this.appHeader});
}
组件.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
解决方案
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 的更多操作,请检查此
推荐阅读
- r - 基于 R 中的其他两个变量的条件赋值
- reactjs - http 代理中间件未在我的反应应用程序中创建代理
- excel - 如何在“IF 语句”中调用 VBA 中的多个 Sheet.Names?
- python - MKDocs 服务的 Python 3.9 错误
- angular - 添加插件 firebasex 后的 Ionic cordova 构建问题
- javascript - ChartJS - 在每个条的中心显示值
- sql - 尝试使用 sql 或 linq 查询从 3 个不同的表中获取结果
- java - 如何将以下字符串流减少为布尔值
- vuejs2 - 如何将我的静态 CSS 与 VueJS 和 webpack 一起使用
- javascript - 如何在网站上保留切换状态?