angular - 在组件angular7中显示当前用户的用户名
问题描述
我在身份验证服务中创建了一个发出用户全部数据的主题。在标题组件中,我订阅了那个 observable。一切都应该正常,但它没有显示任何数据。这是 auth 服务中的 autoAuth 功能。
autoAuthUser() {
const authInformation = this.getAuthData();
if (!authInformation) {
return;
}
this.authStatusListener.next(true);
this.fetchUser(authInformation.userId).subscribe(
(res) => {
if (res.user) {
this.currentUser = res.user;
this.userListener.next(this.currentUser);
}
}
);
}
}
这是在标题组件中:
this.authService.autoAuthUser();
this.authService.getAuthStatusListener().subscribe((isA) => {
this.isAuthenticated = isA;
if (this.isAuthenticated) {
this.authService.getUserListener().subscribe(
(user) => {
this.currentUser = user;
}
);
}
这是在标题组件的模板中:
<p class="username">{{currentUser?.username}}</p>
当我在标题组件中控制台记录当前用户时,我得到了整个正确的数据,但它没有显示任何内容。
解决方案
在标题组件中,将this.authService.autoAuthUser();
调用移动到底部。
这应该工作
this.authService.getUserListener().subscribe((user) => {
this.currentUser = user;
});
this.authService.getAuthStatusListener().subscribe((isA) => {
this.isAuthenticated = isA;
});
this.authService.autoAuthUser();
推荐阅读
- google-app-maker - 显示标签基于,一个数据源(单数)上的字段在另一个数据源字段中
- amp-html - amp-date-picker 返回不正确的日期
- android - 有没有办法在 Android 中自动打开浏览器选项卡?
- oracle - 如何从一台电脑转移到另一台电脑
- javascript - 在嵌套的 flexbox 列中滚动内容(具有动态高度)
- .htaccess - Opencart 不重定向未找到的链接
- jasmine - 无法在自定义 jasmine 报告器中写出量角器网络/性能日志
- c# - 如何设置图像的 200x200 背景?
- leaflet - 来自标记的多个弹出窗口
- memory-management - Dask drop 重复内存错误 split_out