有人可以告诉我在项目中使用多个模块时设置路由的正确性吗?我有app.module和courses.module,其中声明了一些组件。我想知道如何连接模块并在courses.module中正确编辑路由,即共享路由:" / courses / list"和" / courses / detail"
app.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
//import { CoursesRoutingModule } from './components/courses/courses-routing.module';
const routes: Routes = [
{
path: 'courses',
loadChildren: './components/courses/courses-routing.module#CoursesRoutingModule'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.component.html
<h1>App.component</h1>
<p>
<button routerLink="/">HOME</button>
<button routerLink="/courses">KURSY</button>
</p>
<router-outlet></router-outlet>
这里的课程内容:
courses.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CoursesComponent } from './courses.component';
import { CoursesDetailComponent } from './components/courses-detail/courses-detail.component';
import { CoursesListComponent } from './components/courses-list/courses-list.component';
const routes: Routes = [
// {
// path: 'courses',
// loadChildren: '/src/app/components/courses/courses.module'
// }
// ,
// {
// path: 'courses/list',
// component: CoursesListComponent,
// outlet: 'courseslist'
// }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class CoursesRoutingModule { }
courses.component.html:
<p>
<button routerLink="/">HOME</button>
<button routerLink="/courses/list">list</button>
<button routerLink="/courses/detail">detail</button>
</p>
答案 0 :(得分:30)
我是这样做的:
<强> app.module.ts 强>
import {ModuleRouting} from './app.routing';
@NgModule({
declarations: [
AppComponent,
],
imports: [
ModuleRouting,
SubmoduleModule
]
bootstrap: [AppComponent]
})
export class AppModule {
}
<强> app.routing.ts 强>
import {Routes, RouterModule} from '@angular/router';
import {ModuleWithProviders} from '@angular/core';
const routes: Routes = [
{path: 'submodule', loadChildren: 'app/submodule/submodule.module#SubModule'},
];
export const ModuleRouting: ModuleWithProviders = RouterModule.forRoot(routes);
<强> submodule.module.ts 强>
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {ModuleRouting} from './submodule.routing';
@NgModule({
imports: [
CommonModule,
ModuleRouting
],
declarations: [
//...
]
})
export class SubmoduleModule {
}
<强> submodule.routing.ts 强>
import {RouterModule, Routes} from '@angular/router';
import {ModuleWithProviders} from '@angular/core';
const routes: Routes = [
{
path: '',
component: SomeComponent,
},
{
path: 'other',
component: SomeOtherComponent,
}
];
export const ModuleRouting: ModuleWithProviders = RouterModule.forChild(routes);
答案 1 :(得分:2)
app.routing.module.ts
const routes: Routes = [
{
path: '',
children: [
{ path: 'courses', loadChildren: './components/courses/courses-routing.module#CoursesRoutingModule' }
]
}
];
courses.routing.module.ts
const routes: Routes = [
{
path: '',
children: [
{ path: 'list', component: CoursesListComponent}
}
}
];
我会这样做。亲自尝试一下,看看它是怎么回事。