下面是一个example:ExpressionChangedAfterItHasBeenCheckedError双向角结合
@Component({
selector: 'my-app',
template: `
<div>
<h1>{{ foo }}</h1>
<bpp [(foo)]="foo"></bpp>
</div>
`,
})
export class App {
foo;
}
@Component({
selector: 'bpp',
template: `
<div>
<h2>{{ foo }}</h2>
</div>
`,
})
export class Bpp {
@Input('foo') foo;
@Output('fooChange') fooChange = new EventEmitter();
ngAfterViewInit() {
const potentiallyButNotNecessarilyAsyncObservable = Observable.of(null);
potentiallyButNotNecessarilyAsyncObservable.subscribe(() => {
this.fooChange.emit('foo');
})
}
}
其中的错误,偶尔会出现:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'foo'
它的事实,双向绑定是通过观察到更新的结果,可以在相同的勾号上获得价值。我宁愿不用上面的逻辑来包装setTimeout
,因为它看起来像一个黑客行为并使控制流程变得复杂了。
在这里可以做些什么来避免这个错误?
ExpressionChangedAfterItHasBeenCheckedError
错误是否有不良影响或可以忽略?如果可以的话,可以改变探测器对其进行沉默而不污染控制台?
我从来没有得到那个错误,但你有没有尝试过使用异步管道?可能会有所帮助。 – fastAsTortoise
@fastAsTortoise此处不适用管道无论模板如何,都会显示错误。 – estus