【发布时间】:2019-04-03 01:56:06
【问题描述】:
我是 Angular2/4 的新手。在我的界面中,详细信息获取和保存工作正常。但是当我们刷新界面时,获取的详细信息就消失了。我们如何在刷新后不丢失界面细节的情况下解决这个问题
这是我的 Login.component.ts 文件
btnlogin() {
this.message = '';
this.storage.clear();
if (this.validate() == true) {
this._enqService.FetchUserDetails(this.username, this.password).subscribe(userData => {
if (userData.length == 0)
this.message = " * Username/Password not correct";
else {
userData.forEach(item => {
this._loginService._userid = item.UserID;
this._loginService._username = item.UserName;
//if (this._loginService.selectedshopid == undefined || this._loginService.selectedshopid == 0) {
// this.messages = '* Please Select a Shop';
// return false;
//}
this._router.navigate(['/enquirydetails']);
});
}
},
error => {
console.error(error);
this.statusMessage = "Problem with the service.Please try again after sometime";
});
}
}
validate(): boolean {
if (this.username == '' || this.password == '' || this.username == undefined || this.password == undefined) {
this.message = '* Please enter username/password';
return false;
}
else
return true;
}
这是我的 logoutcomponent.ts
constructor(private router: Router, location: Location, private storage:
LocalStorageService)
{
this.location = location;
router.events.subscribe((val) => {
if (this.location.path() != '') {
this.route = location.path();
}
});
}
setUserName()
{
//alert('calling setusername');
this.UserName = this.storage.retrieve('usernamevalue');
}
logout() {
this.router.navigate(['Login']);
this.storage.clear('boundvalue');
this.storage.clear('uservalue');
this.storage.clear();
//alert(this.storage.retrieve('boundvalue'));
//alert(this.storage.retrieve('uservalue'));
}
// ngOnInit() {
// this.router.navigate([''])
// }
}
【问题讨论】:
-
(我想要这样,登录的用户详细信息存储在本地存储中,因此如果用户刷新浏览器以及浏览器会话之间的用户将保持登录状态,直到他们注销)
标签: angular typescript angular2-services angular4-router userid