【发布时间】:2019-04-26 05:16:01
【问题描述】:
我有如下界面:
interface Drawer {
title: string,
content: Component
}
然后我实例化这个接口:
let workspace: Drawer = {
title: 'Workspaces',
content: SidebarWorkspacesComponent
};
在编译过程中出现以下错误:
ERROR in src/app/components/sidebar/sidebar-toggler.component.ts(36,4): error TS2559: Type 'typeof SidebarWorkspacesComponent' has no properties in common with type 'Component'.
现在我尝试使用 ComponentRef 并阅读了数十篇文章,但无法弄清楚如何在接口中传递组件。显然我可以简单地将内容声明为“任何”,但我更愿意知道如何正确地做事。
提前致谢!
【问题讨论】:
-
无需进一步依赖即可获得的最接近的东西。是
content: Type<any>使用Type from @angular/core,因为组件并不真正共享一个公共接口,除非你强制它。 -
@Jota.Toledo 你应该这样回答
-
@PierreDuc 完成,希望对您有所帮助
-
@SiddAjmera 基本上与我在回答中描述的占位符界面方法相匹配。它是一个解决方案,但 IMO 没有增加任何价值,因为它是一个空界面。
-
刚刚为此添加了评论。非常感谢您的意见。
标签: angular typescript angular-components