mda*_*shs 7 routing ng-modules 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
Run Code Online (Sandbox Code Playgroud)
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 { }
Run Code Online (Sandbox Code Playgroud)
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 { }
Run Code Online (Sandbox Code Playgroud)
家庭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 { }
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
Cla*_*tin 20
您需要HomeModule在该imports部分中正确导入,而不是declarations:
@NgModule({
imports: [BrowserModule, routing, HttpModule, ReactiveFormsModule, HomeModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [UserAuthenticationService]
})
export class AppModule {
}
Run Code Online (Sandbox Code Playgroud)
我建议这个物品,其精美的解释@NgModule事
| 归档时间: |
|
| 查看次数: |
14450 次 |
| 最近记录: |