如何在任何模块或组件中使用组件模板?

猛禽丙烯酸

我创建了一个自定义标头,希望将其导入几个组件中。例如:-

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {

    constructor() { }

    ngOnInit(): void { }

}

这是组件,我想在其他模块中的其他组件中使用它。如何使用选择器将其导入HTML。我想将其用作可重用组件。该组件被导入到app.module.ts中,每次我使用选择器将其导入另一个模块时,都会出现此错误:

ERROR in src/app/home/home.component.html:1:1 - error NG8001: 'app-header' is not a known element:
1. If 'app-header' is an Angular component, then verify that it is part of this module.
2. If 'app-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

我尝试将标头导入单独的模块的module.ts中,但它给出了相同的错误。

帕迪普·Ja那

不,您不能这样使用。

如果要在多个模块中使用某些共享组件,则必须首先为共享组件创建模块,然后才必须为多个模块使用该组件。

示例-为单个组件创建模块,app.header.module.ts然后app-header在该模块中导出您的组件,app.header.module.ts然后在其他模块中导入Module,然后只有您可以将此组件用作共享组件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章