angular - Angular,@ViewChild 在构造函数/ngOninit 中子组件的值发生变化时不会更新
问题描述
下面,我不明白为什么 console.log("My status :",status); 在子属性发生更改时未显示,这是我
在构造函数中的子组件我有这个
@Output() public isLoged :Subject<boolean> = new Subject();
constructor(
private authService :AuthService,
) {
authService.getLoggedStatus.subscribe(status => this.isLoged.next(status) );
if(this.authService.isLoggedIn()){
console.log(" -----Logged---- ");
this.isLoged.next(true);
this.authService.getLoggedStatus.next(true);
}
}
和我的父 component.ts
logged :boolean ;
@ViewChild(LoginComponent) login:LoginComponent;
ngAfterViewInit() {
this.login.isLoged.subscribe(status => this.getChildProperty(status));
}
getChildProperty(status) {
this.logged = status;
console.log("My status :",status);
}
所以控制台中的结果就是 => ** -----Logged---- **
但我想要的是** -----记录----
我的状态:是的**
注意:结果显示以防万一代码(在构造函数中的 childComp 中)位于 Onclock 按钮中!
解决方案
谢谢大家的回复,我通过添加解决了我的问题
行为主体
而不是Subject
.
推荐阅读
- javascript - ngrx 推迟一个有效的动作,直到另一个动作被分派
- php - Codigniter flashdata 有时不起作用
- mongodb - 如何从 MongoDB 字段中的字符串中删除特殊字符?
- python - 如何在 python 中编码视频响应?
- wordpress - 如何删除或禁用 wordpress 上的移动菜单?
- azure-devops - ArchiveFiles@2 和根文件夹
- python - 如何在 Pandas 中设置合并金额的小幅度差异
- c++ - main.cpp:3:1:注意:“std::thread”在标题中定义
'; 你忘了'#include '? - javascript - MongoDB:创建包含当前时间戳的复合_id
- chromebook - 我故意删除了 sudo 并且终端要求我输入 sudo passwd