简体   繁体   English

不能在模块声明中使用抽象 class Angular 组件

[英]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.

相关问题 如何在抽象类和 Angular 8 中抽象类的实现中使用@Component 装饰器? - How to use @Component decorator in both abstract class and in the implementation of abstract class in Angular 8? Angular AOT编译错误“无法确定类组件的模块” - Angular AOT compilation error "cannot determine module for class Component'' 测试 Angular 组件扩展抽象 class - Testing Angular component extending an abstract class Angular 库模块使用抽象类注入服务 - Angular library module inject service with abstract class 找不到模块 Angular 2 @Component - Cannot find module Angular 2 @Component 无法确定组件角度 5 的模块 - cannot determine the module for component angular 5 Angular:无法在来自不同模块的组件中使用在应用模块中导入的模块 - Angular: Cannot use module imported in app module in a component from a different module Angular 2-无法在组件声明中导入另一个组件 - Angular 2-Cannot import another component in component declaration Angular 7-生产构建错误-无法确定X类组件的模块! 将X组件添加到NgModule中进行修复 - Angular 7 - Production Build Error - Cannot determine the module for class X Component! Add X Component to the NgModule to fix it Angular 2 angular-cli AOT在模块中声明抽象类? - Angular 2 angular-cli AOT declare abstract class in module?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM