Javascript 如何存储从API检索的值并在整个应用程序中使用它
我想知道如何存储从API检索到的值,并在连接时在整个应用程序中使用它Javascript 如何存储从API检索的值并在整个应用程序中使用它,javascript,angular,api,Javascript,Angular,Api,我想知道如何存储从API检索到的值,并在连接时在整个应用程序中使用它 verifieruserConnect(){ let headers=new HttpHeaders({'Authorization':this.jwt}); return this.http.get(this.BaseUrl+'/verfieruserConnection',{headers:headers}).subscribe(data=>{ this.userConne
verifieruserConnect(){
let headers=new HttpHeaders({'Authorization':this.jwt});
return this.http.get(this.BaseUrl+'/verfieruserConnection',{headers:headers}).subscribe(data=>{
this.userConnected=data;
});
}
我想在整个应用程序中使用this.userConnected
感谢您的帮助创建一个服务来存储此值,并使用该服务随时随地获取该值。 像这样的
import { Injectable } from "@angular/core";
@Injectable({
providedIn: 'root',
})
export class UserService {
private isUserConneted: boolean = false;
saveUserStatus(status: boolean) {
this.isUserConneted = status;
}
getUserStatus(): boolean {
return this.isUserConneted;
}
}
您可以使用localstorage来存储它。您也可以尝试使用redux或其他一些状态管理器。此函数在服务中使用,但我希望在auth guardok中使用此值的问题…可能是创建差异服务来存储此值。将这个新服务注入authGaurd并调用getUserStatus函数来获取状态。