我的组件具有依赖于当前日期时间的样式。在我的组件中,我有如下函数。
private fontColor( dto : Dto ) : string {
// date d'exécution du dto
let dtoDate : Date = new Date( dto.LastExecution );
(...)
let color = "hsl( " + hue + ", 80%, " + (maxLigness - lightnessAmp) + "%)";
return color;
}
lightnessAmp从当前datetime开始计算。如果dtoDate在过去24小时内,则颜色会发生变化。
准确的错误如下:
表达式在检查后发生了变化。先前值:'hsl(123, 80%, 49%)'。当前值:'hsl(123, 80%, 48%)'
我知道异常只出现在开发模式的值被检查的时刻。如果检查值与更新值不同,则抛出异常。
因此,我尝试在以下钩子方法中更新每个生命周期的当前datetime,以防止异常:
ngAfterViewChecked()
{
console.log( "! changement de la date du composant !" );
this.dateNow = new Date();
}
...但是没有成功。
尽管已经有很多答案,并且有一篇关于变更检测的很好的文章的链接,但我还是想在这里发表我的意见。我认为检查是有原因的,所以我考虑了我的应用程序的架构,并意识到视图中的变化可以通过使用行为主体和正确的生命周期钩子来处理。这就是我的解。
I use a third-party component (fullcalendar), but I also use Angular Material, so although I made a new plugin for styling, getting the look and feel was a bit awkward because customization of the calendar header is not possible without forking the repo and rolling your own.
So I ended up getting the underlying JavaScript class, and need to initialize my own calendar header for the component. That requires the ViewChild to be rendered befor my parent is rendered, which is not the way Angular works. This is why I wrapped the value I need for my template in a BehaviourSubject<View>(null):
calendarView$ = new BehaviorSubject<View>(null);
接下来,当我可以确定视图被检查时,我用@ViewChild中的值更新该主题:
ngAfterViewInit(): void {
// ViewChild is available here, so get the JS API
this.calendarApi = this.calendar.getApi();
}
ngAfterViewChecked(): void {
// The view has been checked and I know that the View object from
// fullcalendar is available, so emit it.
this.calendarView$.next(this.calendarApi.view);
}
然后,在我的模板中,我只使用异步管道。没有黑客与变更检测,没有错误,工作顺利。
如果你需要更多细节,请尽管问。