angular - 即使在Angular中刷新后如何保存数据
问题描述
在刷新之前我设法获取可观察数据并在 html 中显示它但是如果我刷新页面我会丢失所有可观察数据,有没有办法保存状态并且即使在页面刷新之后也能够获取数据。
这是我的共享服务
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { Supplier } from '../components/selectsupplier/selectsupplierObj';
@Injectable({
providedIn: 'root'
})
export class SharedService {
private availablesuppliers = new BehaviorSubject<Supplier[]>(null);
constructor() { }
loadSuppliers(suppliers: Supplier[]){
this.availablesuppliers.next(suppliers);
}
getSuppliers(): Observable<Supplier[]> {
return this.availablesuppliers.asObservable();
}
}
这是我获取数据并显示它的组件
getSupplierID(){
let supplierID = this.route.snapshot.paramMap.get('id');
return (supplierID);
}
ngOnInit() {
// call function on page load
this.report();
this.SupplierID = this.getSupplierID();
// console.log(typeof(this.SupplierID));
// localStorage.setItem('supplierid', this.SupplierID);
this.Auth.authStatus.subscribe(value => this.loggedIn = value);
this.shared.getSuppliers()
.pipe(
map(suppliers =>
// console.log("suppliers", suppliers)
suppliers.filter(supplier => supplier.SupplierID === this.SupplierID),take(1),
)
)
.subscribe(response => {
// console.log("From shared services", response);
// console.log(this.SupplierID);
this.supplier = response;
});
所以在第一次运行供应商时填充了数据,但是当我刷新供应商时返回为空。
更新代码,将数据保存在本地存储中,然后检索它以显示
ngOnInit() {
// call function on page load
this.report();
this.SupplierID = this.getSupplierID();
// console.log(typeof(this.SupplierID));
// localStorage.setItem('supplierid', this.SupplierID);
this.Auth.authStatus.subscribe(value => this.loggedIn = value);
this.shared.getSuppliers()
.pipe(
map(suppliers =>
// console.log("suppliers", suppliers)
suppliers.filter(supplier => supplier.SupplierID === this.SupplierID),take(1),
)
)
.subscribe(response => {
// console.log("From shared services", response);
// console.log(this.SupplierID);
// this.supplier = response;
localStorage.setItem('supplierObject', JSON.stringify(response));
var retrievedObject = localStorage.getItem('supplierObject');
this.supplier = JSON.parse(retrievedObject);
console.log("Object from localStorage", this.supplier);
});
}
解决方案
谢谢大家,在您的帮助下,我设法存储在 localStorage 并修复了错误并重构了代码
getSupplierID(){
let supplierID = this.route.snapshot.paramMap.get('id');
return (supplierID);
}
ngOnInit() {
//call function on page load
this.Numeric();
this.SupplierID = this.getSupplierID();
//fnc call
this.service(this.SupplierID);
//retrive object and store in supplier
var retrievedObject = localStorage.getItem('supplierObject');
this.supplier = JSON.parse(retrievedObject);
}
//get the observable and store in localStorage
service(id){
this.Auth.authStatus.subscribe(value => this.loggedIn = value);
this.shared.getSuppliers()
.pipe(
map(suppliers =>
// console.log("suppliers", suppliers)
suppliers.filter(supplier => supplier.SupplierID === id),
)
)
.subscribe(response => {
// console.log("From shared services", response);
// console.log(this.SupplierID);
// this.supplier = response;
localStorage.setItem('supplierObject', JSON.stringify(response));
});
}
推荐阅读
- r - 使用 R,如何根据要选择的列名称的一列从不同列中选择值?
- python - 如何在启动时从 Ubuntu 启动 Eclipse
- java - 在 .xml 文件设计中没有显示在整个项目中
- django - Django ORM:检查字段是空白还是填充
- wordpress - 如果用户未登录,则 WordPress 重定向
- python - 使用正则表达式将多个单词与文本匹配
- angular - 无法在库中使用 cdkDropConnectedTo 属性
- c++ - 这是什么意思?
- deep-learning - 如何在 PyTorch 中将 RGB 图像编码为 n_class One 热张量
- javascript - 将应用程序从 Angular 5 更新到 Angular 8 会导致样式问题