gpt4 book ai didi

Angular 2 - AppModule 声明的意外模块

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

我是 Angular 的新手,我试图在具有以下目录结构的 Angular2 应用程序中分离我的模块。

我在 AppModule 中声明了我的模块和其他组件,但是我在浏览器控制台中收到一个错误 Unexpected HomeModule declared by AppModule

app
--authentication
---- htmls, ts, css
--home
----dashboard
--------html, ts, css
----representativs
--------html, ts, css
----home-routing.module.ts
----home.module.ts
--app.routing.ts
--app.module.ts

应用程序模块.ts

import { routing } from "./app.routing"
import { AppComponent } from './app.component';
import { HomeModule } from "./home/home.module";

@NgModule({
imports: [BrowserModule, routing, HttpModule, ReactiveFormsModule],
declarations: [ AppComponent, HomeModule],
bootstrap: [AppComponent],
providers: [UserAuthenticationService]
})
export class AppModule { }

home.module.ts

import { NgModule } from '@angular/core';
import { DashboardComponent } from './dashboard/dashboard.component';
import { RepresentativesComponent } from './representatives/representatives.component';
import { HomeRoutingModule } from "./home-routing.module";

@NgModule({
imports: [
HomeRoutingModule
],
declarations: [
DashboardComponent,
RepresentativesComponent,
]
})
export class HomeModule { }

home-routing.ts

const homeRoutes: Routes = [
{
path: 'home',
component: HomeComponent,
children: [
{
path: "representatives",
component: RepresentativesComponent
},
{
path: "dashboard",
component: DashboardComponent
},
{
path: "",
redirectTo: "dashboard",
pathMatch: "full"
}
]
}
]

@NgModule({
imports: [
RouterModule.forChild(homeRoutes)
],
exports: [
RouterModule
]
})
export class HomeRoutingModule { }

应用程序路由.ts

import { AuthenticationComponent } from "./authentication/authentication.component";
import { HomeComponent } from "./home/home.component";

const routes: Routes = [
{
path: 'auth/:action',
component: AuthenticationComponent
},
{
path: 'auth',
redirectTo: 'auth/signin',
pathMatch: 'prefix'
},
{
path: '',
redirectTo: 'home',
component: HomeComponent
}
]

export const routing = RouterModule.forRoot(routes);

最佳答案

您需要在 imports 部分正确导入您的 HomeModule,而不是 declarations:

@NgModule({
imports: [BrowserModule, routing, HttpModule, ReactiveFormsModule, HomeModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [UserAuthenticationService]
})
export class AppModule {
}

我推荐this漂亮地解释了@NgModule 的文章

关于Angular 2 - AppModule 声明的意外模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40911841/

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