【发布时间】:2018-10-03 16:03:28
【问题描述】:
我有这个组件。
constructor(private cs : CredentialsService) {
this.fullname = this.cs.getFullname();
this.address = this.cs.getAddress();
this.telp = this.cs.getTelp();
this.photo = this.cs.getPhoto();
if(this.photo == ""){
this.photo = "https://via.placeholder.com/150x150"
}else{
this.photo = "http://localhost:84/ELEARNINGAPI/upload/" + this.photo
} this.email = this.cs.getEmail();
}
还有这个服务CredentialsService(从本地存储用户信息)。
export class CredentialsService {
credentials : any;
constructor(private apiservice : LoginapiService) {
this.credentials = JSON.parse(this.apiservice.getLogginCredentials());
if(this.credentials == null){
this.apiservice.logout();
}
}
getFullname(){
return this.credentials.fullname;
}
getEmail(){
return this.credentials.email;
}
getAddress(){
return this.credentials.address;
}
getTelp(){
return this.credentials.telpno;
}
getPhoto(){
return this.credentials.profilpict;
}
}
这是我的apiservice.getLogginCredentials()
getLogginCredentials(){
return localStorage.getItem("Credentials");
}
正如您在我的component constructor 中看到的那样,我从 CredientialsService 获得了价值。这就是我设置 CredentialsService 的方式
this.loginapi.setLogginCredentials("somejson");
--
setLogginCredentials(UserInfo){
localStorage.setItem("Credentials" , JSON.stringify(UserInfo));
}
我有一个update component 可以用新值重新设置setLogginCredentials。所以我的问题是当我用新值更新值时,我仍然需要刷新整个页面来刷新组件构造函数中的值。
【问题讨论】: