我正在将代码从Angular 5升级到Angular 8,并且我面临着一个奇怪的问题(scrollContainer和scrollWrapper在ngAfterViewInit中都没有定义):
component.ts
@ViewChild('timelineScrollContainer',{ static: true}) set controlElRef(elementRef: ElementRef) {
this.scrollContainer = elementRef;
}
@ViewChild('timelineScroll',{ static: true}) set controlElWrapRef(elementRef: ElementRef) {
this.scrollWrapper = elementRef;
}
ngOnInit() {
this.initTimelineMonths();
this.isLoaded = true;
}
initTimelineMonths() {
//Populate months array
this.months = [];
}
ngAfterViewInit() {
//scrollTimeLineToItem uses the elementRef of scrollContainer
this.scrollTimeLineToItem(this.monthBoxComponents.last, false);
}
component.html
<div class="timeline-scroll-wrapper" #timelineScroll *ngIf="months">
<div class="bar-labels label-left">
<span>Federal</span>
<span>State</span>
<span>24 Month</span>
</div>
<div class="bar-labels label-right">
<span>Federal</span>
<span>State</span>
<span>24 Month</span>
</div>
<div class="timeline-scroll-container" #timelineScrollContainer>
<app-month-box *ngFor="let item of months; let i = index;"></app-month-box>
</div>
这里出了什么问题?感谢您的投入。
谢谢
发布于 2019-12-02 23:41:00
在您的component.ts
中,执行以下更改。
@ViewChild('timelineScroll', { static: false }) divTimelineScrollRef: ElementRef<HTMLDivElement>;
@ViewChild('timelineScrollContainer', { static: false }) divTimelineScrollContainerRef: ElementRef<HTMLDivElement>;
...
https://stackoverflow.com/questions/59142005
复制相似问题