ngAfterViewInit是否无法在角度6中正常工作?

Nin*_*sin 5 javascript angular angular6

我正在浏览angular6 docs->组件交互部分,特别是“父级调用@ViewChild()”。它说-在Angular显示父视图之后,计时器组件才可用。因此最初显示为0秒。但是,当我尝试在ngAfterViewInit之外使用它时,它工作正常。见下文:

seconds() { return this.timerComponent.seconds; }

ngAfterViewInit() {
// Redefine `seconds()` to get from the `CountdownTimerComponent.seconds` ...
// but wait a tick first to avoid one-time devMode
// unidirectional-data-flow-violation error
// setTimeout(() => this.seconds = () => this.timerComponent.seconds, 0);
}
Run Code Online (Sandbox Code Playgroud)

此外,他们还提到要在同一周期内更新父级视图之前要稍等片刻,以避免编译器错误,但我也能够做到这一点。见下文:

seconds() { return 0; }

ngAfterViewInit() {
// Redefine `seconds()` to get from the `CountdownTimerComponent.seconds` ...
// but wait a tick first to avoid one-time devMode
// unidirectional-data-flow-violation error
// setTimeout(() => this.seconds = () => this.timerComponent.seconds, 0);
this.seconds = () => this.timerComponent.seconds;
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释发生了什么吗?

PS链接:https : //angular.io/guide/component-interaction#parent-calls-an-viewchild https://stackblitz.com/angular/pxxnmnlgadmk?file=src%2Fapp%2Fcountdown-parent.component.ts