我正在努力跟踪https://material.angular.io/components/component/dialog上的文档,但我不明白为什么会出现以下问题?

我在我的组件上添加了以下内容:

@Component({
  selector: 'dialog-result-example-dialog',
  templateUrl: './dialog-result-example-dialog.html',
})
export class DialogResultExampleDialog {
  constructor(public dialogRef: MdDialogRef<DialogResultExampleDialog>) {}
}

在我的模块中,我添加了

import { HomeComponent,DialogResultExampleDialog } from './home/home.component';

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    DashboardComponent,
    HomeComponent,
    DialogResultExampleDialog
  ],

// ...

然而,我得到了这个错误……

EXCEPTION: Error in ./HomeComponent class HomeComponent - inline template:53:0 caused by: No component factory found for DialogResultExampleDialog. Did you add it to @NgModule.entryComponents?
    ErrorHandler.handleError @ error_handler.js:50
    next @ application_ref.js:346
    schedulerFn @ async.js:91
    SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
    SafeSubscriber.next @ Subscriber.js:172
    Subscriber._next @ Subscriber.js:125
    Subscriber.next @ Subscriber.js:89
    Subject.next @ Subject.js:55
    EventEmitter.emit @ async.js:77
    NgZone.triggerError @ ng_zone.js:329
    onHandleError @ ng_zone.js:290
    ZoneDelegate.handleError @ zone.js:246
    Zone.runTask @ zone.js:154
    ZoneTask.invoke @ zone.js:345
    error_handler.js:52 ORIGINAL EXCEPTION: No component factory found for DialogResultExampleDialog. Did you add it to @NgModule.entryComponents?
    ErrorHandler.handleError @ error_handler.js:52
    next @ application_ref.js:346
    schedulerFn @ async.js:91
    SafeSubscriber.__tryOrUnsub @ Subscriber.js:223
    SafeSubscriber.next @ Subscriber.js:172
    Subscriber._next @ Subscriber.js:125
    Subscriber.next @ Subscriber.js:89
    Subject.next @ Subject.js:55
    EventEmitter.emit @ async.js:77
    NgZone.triggerError @ ng_zone.js:329
    onHandleError @ ng_zone.js:290
    ZoneDelegate.handleError @ zone.js:246
    Zone.runTask @ zone.js:154
    ZoneTask.invoke @ zone.js:345

推荐答案

Angular 9.0.0 <

从Ivy的9.0.0版本开始,不再需要entryComponents属性.请参见deprecations guide.

Angular 9.0.0 >

您需要将动态创建的组件添加到@NgModule中的entryComponents

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    DashboardComponent,
    HomeComponent,
    DialogResultExampleDialog        
  ],
  entryComponents: [DialogResultExampleDialog]

Note:在某些情况下,lazy loaded modules下的entryComponents将不起作用,因为变通办法是将它们放入app.module(Root)

Angular相关问答推荐

当我试图将日期放在x轴上时,Angular Highcharts区域平面图不工作

如何取消针叶林输入的自动填充?

日语字符不受角形约束控制

*ngFor循环中令人困惑的Angular 行为

iOS Mobile Safari - HTML5视频覆盖一切

Primeng 12Angular 12自定义库:错误符号字段集声明于...在编译保存后未从Primeng/fieldset中导出

我如何更新此Ionic应用程序以解决路由问题?

确保我的角库S服务构建提供独立的友好提供功能

RxJS轮询+使用退避策略重试

ION标签-从子对象中加载页,保留父对象的S内容

完成行为主体

错误:在@angular/core中找不到导出ɵivyEnabled(导入为ɵivyEnabled)

RxJs 中的空闲可观察对象在幕后做了什么?

如何访问父组件中提供的 TemplateRef 内的服务?

带有重定向的Angular 2 AuthGuard服务?

如何使用ngrx和effects 订阅成功回调

ngx-bootstrap modal:如何从模态中获取返回值?

如何将组件导入Angular 2中的另一个根组件

Angular 4中的md-select中的onselected事件

如何防止角material垫菜单关闭?