材料对话框不渲染组件?

发布于 2025-02-08 14:33:19 字数 876 浏览 1 评论 0 原文

我有一个简单的组件:

@Component({
  selector: 'my-component',
  template: '<p>I am a simple component</p>',
})

如果我放置&lt; my-component&gt;&lt;/my-component&gt; 在我的应用程序的html中的某个地方,则如预期的那样,我会看到文本:“我是一个简单的组件: “ ...

但是,使用材料对话框...

...

  openDialog () {
    this.dialog.open(ModalComponent, { data: { some: 'data' } });
  }

...    


@Component({
  selector: 'modal',
  template: '<my-component></my-component>',
})
class ModalComponent {
  constructor (
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {}
}

打开此对话框时,我会得到一个空白的对话框。并检查HTML,我基本上看到:

<mat-dialog-container ...(lots of attrs)>
  <modal>
    <my-component></my-component>
  </modal>
</mat-dialog-container>

为什么在此上下文中 my-component 不呈现?

I have a simple component:

@Component({
  selector: 'my-component',
  template: '<p>I am a simple component</p>',
})

If I put <my-component></my-component> somewhere in my app's html, then as expected, I see the text: "I am a simple component" ...

However, using material dialog...

...

  openDialog () {
    this.dialog.open(ModalComponent, { data: { some: 'data' } });
  }

...    


@Component({
  selector: 'modal',
  template: '<my-component></my-component>',
})
class ModalComponent {
  constructor (
    @Inject(MAT_DIALOG_DATA) public data: any
  ) {}
}

When I open this dialog, I get a blank dialog box. And inspecting the HTML, I am basically seeing:

<mat-dialog-container ...(lots of attrs)>
  <modal>
    <my-component></my-component>
  </modal>
</mat-dialog-container>

Why is my-component not rendering in this context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸢与 2025-02-15 14:33:19

这是Stackblitz的链接

在此示例中,组件在对话框中呈现。

将您的My-Component内容导入您的app.module文件。

我希望这就是你的意思吗?

这是一个更新的链接,为您的情况添加所有要共享模块的组件,请确保您正确导出它们。

Here is a link to stackBlitz
https://stackblitz.com/edit/angular-cmrbx5?file=src/app/dialog-overview-example.html

In this example a component renders in dialog.

Import your my-component stuff into your app.module file.

I hope this is what you mean?

enter image description here

Here is an updated link, add all your components you want to shared module for you situation, make sure you are exporting them correctly.

https://stackblitz.com/edit/angular-cmrbx5-pipswr?file=src/app/shared-module.ts

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文