我正在浏览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); …Run Code Online (Sandbox Code Playgroud)