【问题标题】:@ViewChild is null when using Directive Angular使用 Directive Angular 时,@ViewChild 为空
【发布时间】:2018-07-27 09:37:52
【问题描述】:

我正在关注this 关于如何在运行时动态加载组件的教程,但遇到了我的@ViewChildundefined 的问题。

这是我的指令`

import { Directive, ViewContainerRef } from '@angular/core';

@Directive({
    selector: '[dynamic-event]'
})

export class EventDirective {
    constructor(public viewContainerRef: ViewContainerRef) {}
}

组件注入模板的位置(feed.component.html):

<ng-template dynamic-event></ng-template>

feed.component.ts

import { Component, OnInit, ViewChild, ComponentFactoryResolver } from '@angular/core';
import { Event } from '../../models/event';
import { FeedService } from '../../services/feed.service';
import { EventDirective } from '../../directives/event.directive';
import { EventComponent } from '../event.component/event.component';

@Component({
  selector: 'app-feed',
  templateUrl: './feed.component.html',
  styleUrls: ['./feed.component.css']
})
export class FeedComponent implements OnInit {

  //feeds
  feeds: Event[];

  //Get directive which contains ViewContainerRef
  @ViewChild(EventDirective) eventHost: EventDirective;

  constructor(private feedService: FeedService, 
    private componentFactoryResolver: ComponentFactoryResolver) { }

  ngOnInit() {
    //this.getFeeds();
  }
  ngAfterViewChecked() {
    this.getFeeds();
  }

  getFeeds(): void {
    this.feedService
    .getFeeds()
    .subscribe(o => 
      { 
        this.feeds = o;
        this.populateFeeds();
      });
  }
  populateFeeds(): void {
    //resolve my component dynamically 
    let componentFactory = this.componentFactoryResolver.resolveComponentFactory(EventComponent);
    //get FeedComponent's ViewContainer (serves as parent viewContainer)
    let viewContainerRef = this.eventHost.viewContainerRef;
    viewContainerRef.clear();

    if (this.feeds !== undefined || this.feeds !== null || this.feeds.entries.length !== 0){
      this.feeds.forEach(x => {
        let component = viewContainerRef.createComponent(componentFactory);
        (<EventComponent>component.instance).event = x;
      });      
    }
  }
}

我的@ViewChild 未定义,我不知道为什么。有什么帮助吗?

【问题讨论】:

    标签: angular angular-directive viewchild


    【解决方案1】:

    我发现了我的问题。

    因为我没有使用 Angular 的 cli 命令 ng generate directive,它会在 app.module 声明数组中声明我的指令,所以我收到了这个错误。

    我手动添加了我的EventDirective,现在它可以工作了:

    这是一个例子:

    @NgModule({
      declarations: [
        AppComponent,
        FeedComponent,
        EventComponent,
        EventDirective 
      ],
      imports: [
        BrowserModule,
        HttpClientModule
      ],
      providers: [], 
      entryComponents: [EventComponent],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    【讨论】:

      【解决方案2】:

      我认为如果没有实际的原生 HTML 元素,您将无法做到这一点。

      要验证,请使用&lt;div dynamic-event&gt;&lt;/div&gt; 进行验证

      ng-template 是虚拟元素,在使用(某处)之前不会放置在生成的 HTML 标记中 - 因此没有带有指令的元素,字段未定义。

      【讨论】:

      • 我的 feed.component.html 现在看起来像这样:

        这里加载的动态事件

        不幸的是,@ViewChild 仍未定义
      • 更准确地说,ng-template 本身最终会在 html 中被注释,除非它在 ​​ngIf 中被引用
      • @monstertjie_za 点是在像div这样的html元素上设置指令
      • @SurajRao 不知道。您确定这会在生产构建中发生吗?也许它只是处于开发模式?
      • @Antoniossss 我试过
        ,没有解决问题
      猜你喜欢
      • 2020-03-22
      • 1970-01-01
      • 2019-04-11
      • 2015-02-27
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多