1 配置一个路由模块

  1. ng g module views/home --routing //views/home --routing:自动生成并导入
  2. ng g component views/home //home.component组件 配置主组件

二.配置模块中的路由

  1. //home-routing.module.ts
  2. import { HomeComponent } from './home.component';
  3. const routes: Routes = [
  4. {
  5. path:"",
  6. component:HomeComponent
  7. }
  8. ];

三.配置主路由

  1. //app-routing.module.ts
  2. const routes: Routes = [
  3. {
  4. path:"home",
  5. loadChildren:()=>import('./views/home/home.module').then(m=>m.HomeModule)
  6. }
  7. ];

四.在路由模块下配置一个子组件

  1. ng g component views/news/components/night //在home.component组件下新建一个night组件

image.png

五.手动建模块

  1. 1.ng g module views/news
  2. //news.module.ts
  3. @NgModule({
  4. declarations: [NewsComponent],
  5. imports: [
  6. CommonModule,
  7. RouterModule.forChild([
  8. ])
  9. ]
  10. })
  1. 2.ng g component views/news
  2. @NgModule({
  3. declarations: [NewsComponent],
  4. imports: [
  5. CommonModule,
  6. RouterModule.forChild([
  7. {
  8. path:"",
  9. component:NewsComponent
  10. }
  11. ])
  12. ]
  13. })
  1. //app-routing.module.ts
  2. {
  3. path:"news",
  4. loadChildren:()=>import('./views/news/news.module').then(m=>m.NewsModule)
  5. }