【发布时间】:2019-04-03 20:50:31
【问题描述】:
我希望了解将登录后产生的用户对象传递给其他组件的最佳方式。
login() {
this.http.post<Usuario>(this.baseUrl, {
username: this.usuario.username,
clave: this.usuario.clave
}).subscribe(data => {
if (data != null) {
localStorage.setItem('isLoggedIn', 'true');
localStorage.setItem('token', btoa(this.usuario.username + ':' + this.usuario.clave));
this.router.navigate(['/buscador']);
this.usuario2 = data;
console.log(this.usuario2);
} else {
alert('Authentication failed.');
}
});}
这是我登录组件中的登录方法,如果验证正确,因为我正在使用的服务返回一个用户对象,我将用户对象存储到“usuario2”中,我想知道的是最好的方法让其他组件可以访问此变量。
【问题讨论】:
-
签出this answer。
标签: angular