簡體   English   中英

即使在 Angular 8 中的 ngAfterViewInit 上,ViewChild elementRef 也未定義

[英]ViewChild elementRef undefined even on ngAfterViewInit in Angular 8

我正在將代碼從 Angular 5 升級到 Angular 8,但我面臨一個奇怪的問題(scrollContainer 和 scrollWrapper 在 ngAfterViewInit 中都未定義):

組件.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);
}

組件.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>

這里有什么問題? 感謝您的投入。

謝謝

在您的component.ts ,進行以下更改。

@ViewChild('timelineScroll', { static: false }) divTimelineScrollRef: ElementRef<HTMLDivElement>;
@ViewChild('timelineScrollContainer', { static: false }) divTimelineScrollContainerRef: ElementRef<HTMLDivElement>;

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM