angular - 无法使用 ViewContainerRef 读取未定义的属性 'createComponent'

标签 angular typescript

我正在尝试使用@ViewChild 将组件动态注入(inject)到父位置,但出现错误:

Cannot read property 'createComponent' of undefined

在我尝试注入(inject)的 ReferenceTableComponent 组件上

@Component({
selector: 'app-reference-table',
templateUrl: './refTable.component.html',
styleUrls: ['./refTable.component.scss']
})
export class ReferenceTableComponent implements OnInit {

observable: Observable<any>;

@ViewChild('selectorTarget', {read: ViewContainerRef}) selectorTarget;

// colors
@Input() public datas: Array<string>;

public availableColors: Array<string>;

@Input() public nextRow: Array<string>;

//tailles
@Input() public headList: Array<string>;

public rows: Array<any> = [{}];

public rowIDs: Array<any> = [];

constructor(private _cr: ComponentFactoryResolver, private _viewContainerRef: ViewContainerRef) {

}
ngOnInit() {
    this.computeAvailableColors();
}

addRow() {
    console.log('this.availableColors 2: ', this.availableColors)
    this.rows.push({});
}

    computeAvailableColors() {
    this.availableColors = _.concat([''], this.datas);
    this.rowIDs  = _.map(this.rows, 'color')
    this.availableColors = _.difference(this.availableColors, this.rowIDs);
    const select: ComponentRef<SelectOptionsComponent> =
        this.selectorTarget.createComponent(
            this._cr.resolveComponentFactory(SelectOptionsComponent)
        );

    select.instance.availableColors = this.availableColors;
    select.instance.row = this.rows[0];
}

在我的组件 html 上

<td onSelectColor($event) #selectorTarget>
            </td>

我要注入(inject)的组件

@Component({
selector: 'kia-select-options',
template: `<div><select [(ngModel)]="row.color" (ngModelChange)="sendColorEvent(row, $event)"> 
                // value is a string or number
                <option *ngFor="let obj of availableColors">{{obj}}</option>
           </select></div>`
})
export class SelectOptionsComponent {

// couleurs
@Input() public availableColors: Array<string>;

@Input() public row: {};

public color: string;

@Output()
public changed = new EventEmitter();

constructor(private injector: Injector) {
}

sendColorEvent(row, color) {
    console.log(event)
    this.changed.emit({ color: color, row: row });
}
}

知道 ReferenceTableComponent 是从使用的父组件动态自加载的,并且工作正常

  @ViewChild('target', {read: ViewContainerRef}) target;
  const factory = this.componentFactoryResolver.resolveComponentFactory(ReferenceTableComponent);
  const ref = this.target.createComponent(factory);

最佳答案

@ViewChild() 查询在 ngOnInit() 中不可用。仅当 ngAfterViewInit() 被调用时:

ngAfterViewInit() {
    this.computeAvailableColors();
}

关于angular - 无法使用 ViewContainerRef 读取未定义的属性 'createComponent',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45145449/

相关文章:

javascript - 如何使用拓扑图处理 Cytoscape.js Canvas 上的鼠标滚动和按键事件

Angular 2 @Input 经常被调用

javascript - 关于 Angular Material 设计的问题 <mat-chip>

typescript - 计算三角形网格的 SDF 的最有效方法

typescript - 如何在 typescript 中通过 websocket 接收 float 据

angular - 登录我的 angular keycloak 应用程序时无限重定向

angular - 构建 Angular 库时无法读取未定义的属性 'module'

Angular2 : res. json 不是函数

javascript - 在上一个 GET 请求的对象可用后直接发送 GET 请求?

javascript - 使用 componentDidUpdate 更改组件中的状态