首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >即使在Angular 8中的ngAfterViewInit上,ViewChild elementRef也未定义

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

Stack Overflow用户
提问于 2019-12-02 23:25:57
回答 1查看 299关注 0票数 0

我正在将代码从Angular 5升级到Angular 8,并且我面临着一个奇怪的问题(scrollContainer和scrollWrapper在ngAfterViewInit中都没有定义):

component.ts

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
<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>

这里出了什么问题?感谢您的投入。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-02 23:41:00

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

代码语言:javascript
运行
复制
@ViewChild('timelineScroll', { static: false }) divTimelineScrollRef: ElementRef<HTMLDivElement>;
@ViewChild('timelineScrollContainer', { static: false }) divTimelineScrollContainerRef: ElementRef<HTMLDivElement>;

...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59142005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档