简体   繁体   中英

Getting error while displaying a component in lazy loaded module in Angular

I have created a component and its name is s-header. This component is registered in HomeModule because its a part of this module, but when i try to display this component in home.component.html it gives me this erorr.

If 's-header' is an Angular component, then verify that it is part of this module.

If 's-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

here is my code

import { HeaderComponent } from '../../general-components/header/header.component';

declarations: [
    HeaderComponent,
],

this is a header component

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

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

    constructor() { }

    ngOnInit(): void {
    }
}

this is how i am trying to display it

<s-header></s-header>

If this is a version of Angular without Ivy - you'll need to also add this component into the entry component array of the lazy loaded module. Its because the factory for this component doesn't exists (because its coming from a lazy loaded module)

Simply:

import { HeaderComponent } from '../../general-components/header/header.component';

declarations: [
    HeaderComponent,
...
],
entryComponents: [
    HeaderComponent
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM