[英]Cannot use an abstract class Angular component in a module declaration
I have a base component class BaseComponent
with its own markup, let's say this component is <base-component-fun>
.我有一个带有自己标记的基本组件 class
BaseComponent
,假设这个组件是<base-component-fun>
。 Its markup is:它的标记是:
<div>
..Base markup
</div>
I export this component via module and reuse the library in other project when I inherit this component - and have its own markup + base component markup:我通过模块导出此组件并在继承此组件时在其他项目中重用该库 - 并拥有自己的标记 + 基本组件标记:
import { BaseComponent } from 'my-repository/base';
@Component({
...
})
export class InheritedComponent extends BaseComponent
with the markup:带有标记:
<inherited-component-fun>
..Inherited markup
<base-component-fun></base-component-fun>
</inherited-component-fun>
Here comes the problem: I MUST declare my base class BaseComponent
in its module for export:问题来了:我必须在其模块中声明我的基础 class
BaseComponent
以进行导出:
@NgModule({
declarations: [BaseComponent],
otherwise the module is not built.否则不会构建模块。 But declaring an abstract class is not allowed.
但不允许声明抽象 class。
What do I do?我该怎么办? I've tried to find the solution in forums, but I could not.
我试图在论坛中找到解决方案,但我找不到。 So I ended up by making base class non-abstract, which is not very good from design prospective - I want to have some abstract properties ih the base class.
所以我最终使基础 class 非抽象,这从设计的角度来看并不是很好 - 我想在基础 class 中拥有一些抽象属性。
By looking at your code, I see you are trying to render something like this.通过查看您的代码,我看到您正在尝试呈现这样的内容。
component specific template
...
base class/component template
...
component specific template
This doesn't work with the way you are trying to do.这不适用于您尝试做的方式。 You cannot render a class without declaring it in the module and you cannot declare an abstract one.
你不能渲染一个 class 而不在模块中声明它,你不能声明一个抽象的。
Possible solutions,可能的解决方案,
1. Use Content Projection - https://angular.io/guide/content-projection 1.使用内容投影- https://angular.io/guide/content-projection
Make the base class as parent and use the content projection to fill the required sections.将基础 class 作为父级,并使用内容投影来填充所需的部分。 You can put the child components here
您可以将子组件放在这里
2. Use complete template 2.使用完整的模板
If it is a simple structural change (may be like adding a card
class etc), I would suggest you to just use the complete template again.如果是简单的结构更改(可能像添加
card
class 等),我建议您再次使用完整的模板。
3. Use both abstract class and content projection 3. 同时使用抽象 class 和内容投影
Use abstract class for the child component, and use a parent component with content projection to get the proper template.对子组件使用抽象 class,并使用带有内容投影的父组件来获取正确的模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.