8

我是 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

app.module.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 { }

家庭路由.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 { }

app.routing.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);
4

2 回答 2

23

您需要正确导入您HomeModuleimports部分,而不是declarations

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

我推荐这篇文章,它很好地解释了@NgModule 的事情

于 2017-09-21T13:40:28.923 回答
0

如果有人在进行了建议的更正后遇到了同样的问题,则 package.lock.json 可能来自另一个操作系统(例如通过提交)或者不是最新的。因此建议删除 package.lock.json 并发送此命令行:npm install

于 2019-09-17T20:50:30.580 回答