angular - 为什么我看不到 ngOnChanges 记录在我的应用程序中

标签 angular lifecycle

我是 Angular 的新手,正在尝试掌握生命周期 Hook 。但是,我很困惑,因为我在代码中看到的内容与文档不符。

我的组件类中有以下代码:

ngOnInit() {
    console.log("ngOnInit", this.personList);
    this.person = this.pService.getName();
}

ngOnChanges() {
    console.log("ngOnChanges", this.personList, arguments);
}

ngDoCheck() {
    console.log("ngDoCheck", this.personList, arguments);
}

ngAfterContentInit() {
    console.log("ngAfterContentInit", this.personList, arguments);
}

ngAfterViewInit() {
    console.log("ngAfterViewInit", this.personList, arguments);
}

ngAfterViewChecked() {
    console.log("ngAfterViewChecked", this.personList, arguments);
}

ngAfterContentChecked() {
    console.log("ngAfterContentChecked", this.personList, arguments);
}

ngOnDestroy() {
    console.log("ngOnDestroy", this.personList, arguments);
}

changeclr() {
        this.person = this.name === 'Saurabh' ? 'Navanshu' : 'Saurabh';
}

docs这里说 ngOnchangesngonInit 之前触发,但在应用程序加载时我看不到 ngOnChanges 的任何日志。

文档还说 ngOnChnages 在数据绑定(bind)属性更改时运行。它是否特定于使用 @Input 设置的属性,因为当我使用 changclr 方法更改 this.person 时,我看不到任何日志。

我这里有什么问题吗?

最佳答案

ngOnChanges 仅当您的类/组件中有 @Input() 参数时才会触发,它会在任何输入参数值更新时执行。

在您的代码中,您没有提及任何有关输入参数的内容,因此您的 ngOnChanges 未记录。

可能你必须定义如下:@Input() person :any;

关于angular - 为什么我看不到 ngOnChanges 记录在我的应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44363015/

相关文章:

iOS : instance variable in block

c# - 如何检测 Windows 8 何时进入休眠或恢复状态

Angular 4,获取表单数据,编辑和上传

javascript - Angular 服务是否可以直接更改注入(inject)它的组件的属性(没有商店/RxJs)?

Android:创建在应用程序停止时运行的服务

javascript - 使用回调时的 express.js 请求/响应对象生命周期

angular - angular 2 中的 behaviorSubject,它是如何工作的以及如何使用它

javascript - Html 编码 Ionic 2 + API PHP 引用错误

css - 如何将贷款明细和信用明细表放在空白的申请明细下方

ios - 使用 Storyboard时,操作系统何时设置 rootViewController?