【问题标题】:Dialog not working in angular material对话框在角度材料中不起作用
【发布时间】:2018-11-14 03:06:48
【问题描述】:

我正在尝试使用角材料中的dialog。我正在尝试实现页面上给出的第一个示例: Angular Material Dialog URL 这是上页给出的示例的 Stackblitz URL: Example Mentioned on Angular material website

我所做的唯一修改是将对话框组件移动到一个单独的文件夹中。但是对话框没有正确打开,也没有显示数据。 这是我工作的 Stackblitz 链接: My Implementation

我认为对话框组件中没有传递数据。但我无法弄清楚为什么。任何帮助将不胜感激。

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    在您的 Stackblitz 中,单击按钮后控制台会显示有用的错误消息:

    ERROR
    Error: No component factory found for DialogComponentComponent. Did you add it 
    to @NgModule.entryComponents?
    

    要解决这个问题,您需要将对话框中显示的组件添加到 app.module.ts(或 Stackblitz 中的 main.ts 文件)的 entryComponents 集合中:

    entryComponents: [DialogOverviewExample, DialogComponentComponent],

    这是必需的,因为对话框中显示的组件不是通过组件的选择器在模板中引用的。由于 Material 的对话框组件按类型引用了显示的组件,因此 Angular 需要知道该组件仍然需要加载,并且在编译期间不会被摇树。 Angular docs go into some additional detail on entryComponents if you're interested.

    【讨论】:

    • 嗨,谢谢。但是为什么我们需要 DialogComponentComponent 作为 entryComponent?
    • 添加了有关为什么需要这样做的其他信息。
    • 仅供参考 Angular 9+ 不再需要 entryComponents
    【解决方案2】:

    在你的 main.ts 中用 enrtyComponents 声明它:

    ...
    entryComponents: [DialogOverviewExample, DialogComponentComponent],
    ...
    

    here is详细解释。

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 2023-03-10
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 2018-06-17
      • 1970-01-01
      相关资源
      最近更新 更多