gpt4 book ai didi

angular - 组件不是任何 NgModule 的一部分,或者该模块尚未导入到您的模块中

转载 作者:太空狗 更新时间:2023-10-29 16:46:26 26 4
gpt4 key购买 nike

我正在构建一个 Angular 4 应用程序。我收到错误

Error:Component HomeComponent is not part of any NgModule or the module has not been imported into your module.

我已经创建了 HomeModule 和 HomeComponent。 AppModule 需要引用哪一个?我有点困惑。我需要引用 HomeModule 还是 HomeComponent?最终我要寻找的是当用户单击主页菜单时,他应该被定向到将在索引页面上呈现的 home.component.html。

App.module 是:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http'

import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { TopbarComponent } from './topbar/topbar.component';
import { FooterbarComponent } from './footerbar/footerbar.component';
import { MRDBGlobalConstants } from './shared/mrdb.global.constants';
import { AppRoutingModule } from './app.routing';
import { HomeModule } from './Home/home.module';

@NgModule({
declarations: [
AppComponent,
FooterbarComponent,
TopbarComponent,
NavbarComponent

],
imports: [
BrowserModule,
HttpModule,
AppRoutingModule,
HomeModule

],
providers: [MRDBGlobalConstants],
bootstrap: [AppComponent]
})
export class AppModule { }

HomeModule 是:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './home.component';

@NgModule({
imports: [
CommonModule
],
exports: [HomeComponent],
declarations: [HomeComponent]
})
export class HomeModule { }

HomeComponent 是:

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

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

constructor() { }

ngOnInit() {
}

}

最佳答案

如果您不使用延迟加载,则需要在 app.module 中导入 HomeComponent 并在声明中提及它。另外,不要忘记从导入中删除

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpModule} from '@angular/http'

import { AppComponent } from './app.component';
import { NavbarComponent } from './navbar/navbar.component';
import { TopbarComponent } from './topbar/topbar.component';
import { FooterbarComponent } from './footerbar/footerbar.component';
import { MRDBGlobalConstants } from './shared/mrdb.global.constants';
import {AppRoutingModule} from './app.routing';
import {HomeModule} from './Home/home.module';
// import HomeComponent here

@NgModule({
declarations: [
AppComponent,
FooterbarComponent,
TopbarComponent,
NavbarComponent,
// add HomeComponent here
],
imports: [
BrowserModule,
HttpModule,
AppRoutingModule,
HomeModule // remove this

],
providers: [MRDBGlobalConstants],
bootstrap: [AppComponent]
})
export class AppModule { }

关于angular - 组件不是任何 NgModule 的一部分,或者该模块尚未导入到您的模块中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44827999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com