首页 > 解决方案 > ngComponentOutlet 或 ndcDynamicComponent 不适用于角度 7

问题描述

你好我真的很困惑为什么它不工作它很简单我仍然遇到问题。我得到空白输出,如果我直接用它的选择器调用它,我的组件有一些数据正在更新。

这是我的代码。

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeatSealComponent } from './heat-seal/heat-seal.component';
import { PrinterComponent } from './printer/printer.component';
import { DynamicModule } from 'ng-dynamic-component';

@NgModule({
  declarations: [
    AppComponent,
    HeatSealComponent,
    PrinterComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    [DynamicModule],
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html

<ng-container *ngComponentOutlet="dumm"></ng-container>
<ndc-dynamic [ndcDynamicComponent]="dumm"></ndc-dynamic>

app.component.ts

import { element } from 'protractor';
import { Component, OnInit } from '@angular/core';
import { HeatSealComponent } from './heat-seal/heat-seal.component';
import { PrinterComponent } from './printer/printer.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit{
  title = 'dynamic-loading';
  dumm = HeatSealComponent;

  ngOnInit() {

    // this.myContent = {name:'nope'};
  }
}

标签: htmlangulartypescript

解决方案


解决方法是在我的模块文件的EntryComponents中声明组件。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HeatSealComponent } from './heat-seal/heat-seal.component';
import { PrinterComponent } from './printer/printer.component';
import { DynamicModule } from 'ng-dynamic-component';

@NgModule({
  declarations: [
    AppComponent,
    HeatSealComponent,
    PrinterComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    [DynamicModule],
  ],
  providers: [],
  bootstrap: [AppComponent],
  entryComponents: [HeatSealComponent, PrinterComponent], // ADD THIS
})
export class AppModule { }

推荐阅读