Javascript localStorage 在另一个组件中不可用,除非我刷新页面
Javascript localStorage not available in another component unless I refresh the page
编辑:这不是 的副本。我的情况完全不同,事实上,这不是本地存储的问题,而是 Angular 工作方式的问题。
我正在开发 Angular7 应用程序。
我有一个将令牌设置为 localStorage 的组件:window.localStorage.setItem("oauth_token", oauth_token)
然后重定向到主页:this.router.navigate(['/'])
根组件现在应该可以从 localStorage 中读取 window.localStorage.getItem("oauth_token")
,但该值为空,除非我手动刷新页面。
当我检查 Chrome 开发工具时,我可以在本地存储中看到令牌,那么为什么我需要刷新页面?
我该怎么做才能克服这个问题?
试试不用 window,像这样:
localStorage.getItem("oauth_token")...
与本地存储无关。您的根组件需要触发或刷新才能访问本地存储或数据中的任何更改。
当您重定向到主页时,只会执行来自主页组件的代码,而不是来自父组件或根组件的代码。
您需要在服务文件中声明一个变量。因此,无论何时更新本地存储,都必须更新服务文件变量的值。您可以在根组件中访问此变量。因此,如果服务变量发生任何变化,将触发触发器,您可以更新本地存储。检查 link 以获取示例代码 https://github.com/resistcheat/angular-variable-as-observable
创建服务文件
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class CommonService {
data: any;
private items: BehaviorSubject<any> = new BehaviorSubject<any>(this.data);
items$: Observable<any> = this.items.asObservable();
constructor(private http: HttpClient) { }
setLocalStorageData() {// Call this function to set localstorage
localStorage.setItem("oauth_token", oauth_token)
this.data = oauth_token;
}
}
//当this.data变化时触发。将以下代码添加到您的根组件
constructor(private commonService: CommonService) { }
items: any;
subscription: Subscription; //import { Subscription } from 'rxjs';
ngOnInit() {
this.subscription = this.commonService.items$.subscribe(items => {
this.items = JSON.stringify(items));
console.log(localStorage.getItem("oauth_token"));
}
}
ngOnDestroy() {
this.subscription && this.subscription.unsubscribe();
}
编辑:这不是
我正在开发 Angular7 应用程序。
我有一个将令牌设置为 localStorage 的组件:window.localStorage.setItem("oauth_token", oauth_token)
然后重定向到主页:this.router.navigate(['/'])
根组件现在应该可以从 localStorage 中读取 window.localStorage.getItem("oauth_token")
,但该值为空,除非我手动刷新页面。
当我检查 Chrome 开发工具时,我可以在本地存储中看到令牌,那么为什么我需要刷新页面? 我该怎么做才能克服这个问题?
试试不用 window,像这样:
localStorage.getItem("oauth_token")...
与本地存储无关。您的根组件需要触发或刷新才能访问本地存储或数据中的任何更改。
当您重定向到主页时,只会执行来自主页组件的代码,而不是来自父组件或根组件的代码。
您需要在服务文件中声明一个变量。因此,无论何时更新本地存储,都必须更新服务文件变量的值。您可以在根组件中访问此变量。因此,如果服务变量发生任何变化,将触发触发器,您可以更新本地存储。检查 link 以获取示例代码 https://github.com/resistcheat/angular-variable-as-observable
创建服务文件
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class CommonService {
data: any;
private items: BehaviorSubject<any> = new BehaviorSubject<any>(this.data);
items$: Observable<any> = this.items.asObservable();
constructor(private http: HttpClient) { }
setLocalStorageData() {// Call this function to set localstorage
localStorage.setItem("oauth_token", oauth_token)
this.data = oauth_token;
}
}
//当this.data变化时触发。将以下代码添加到您的根组件
constructor(private commonService: CommonService) { }
items: any;
subscription: Subscription; //import { Subscription } from 'rxjs';
ngOnInit() {
this.subscription = this.commonService.items$.subscribe(items => {
this.items = JSON.stringify(items));
console.log(localStorage.getItem("oauth_token"));
}
}
ngOnDestroy() {
this.subscription && this.subscription.unsubscribe();
}