angular - 我的代码中没有触发 Angular 生命周期更改事件
问题描述
我创建了一个用于测试目的的组件来检查ngOnChanges()
火灾与否。当我了解到这ngOnChanges()
是在执行后触发的启动事件Constructor()
,但在我的应用程序中它不起作用。
我的 TS 代码
constructor() {
console.log("constructor");
}
ngOnChanges(){ console.log("ngOnChanges"); }
ngOnInit(){
console.log("ngOnInit");
}
ngOnDestroy(){
console.log("ngOnDestroy");
}
ngDoCheck(){
console.log("ngDoCheck");
}
ngAfterContentInit(){ console.log("ngAfterContentInit"); }
ngAfterContentChecked(){ console.log("ngAfterContentChecked"); }
ngAfterViewInit(){ console.log("ngAfterViewInit"); }
ngAfterViewChecked(){ console.log("ngAfterViewChecked"); }
HTML 页面代码
<h2>Test My Angular Life Cycle</h2>
<input type="buttton" value=""/>
解决方案
仅当组件具有绑定输入并且一个或多个数据绑定输入属性发生更改时,ngOnChanges 才会在 ngOnInit() 之前调用。如果你的组件没有输入或者你在没有提供任何输入的情况下使用它,框架将不会调用 ngOnChanges()。
推荐阅读
- c# - Unity C# 中的 IEnumerable 问题?
- bash - AWK 没有正确地将输入标记为 git 别名的一部分
- sql - 如何在 Microsoft SQL Server 中合并这 3 个表
- angular - 在 Angular 6 中添加 http 标头
- angular - 使用 RxJS 的 Angular 多个 HTTP 请求(不相互依赖)
- android - 设置日期和时间
- python-3.x - pyvenv.cfg 中的 Venv 主页键
- python - 当我使用 phantomjs、python、selenium 创建 pdf 时,pdf 没有完全显示
- c# - 如何使用 gzipstream 将内存流压缩成内存流?
- python-3.x - 如何在不调用 tf.layers.batch_normalization() 的情况下在推理模式下设置批量标准化操作?