【问题标题】:child route configuration with Angular 8Angular 8 的子路由配置
【发布时间】:2019-12-19 10:33:18
【问题描述】:

我有一个 angular 8 的应用,其中 a 创建了两个模块:

  • testModule 和 SimulatorModule

模拟器有路由文件,但 testModule 没有。

我想将模拟器中的所有组件加载为 TestModule 中的 TestComponent 的子组件。

但是当我运行应用程序时期望测试组件是午餐时,我总是重定向到 appComponent。

这些是代码:

**//app.routing.ts**
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
{
 path: '', 
 redirectTo: 'simulator',
 pathMatch: 'full'
},
{
 path: 'simulator',
 loadChildren: './simulator/simulator.module#SimulatorModule'
}
]
 @NgModule({
  imports: [
  RouterModule.forRoot(routes, { scrollPositionRestoration: 'enabled' })
 ],
 exports: [RouterModule],
 providers: []
 })
 export class AppRoutingModule {}

//simulator.routing.module
import { NgModule } from '@angular/core';
import { RouterModule, Routes, Route } from '@angular/router';

import { SimulatorPageComponent } from '@app/simulator/simulator-page/simulator-page.component';
import { Test } from '@app/test/test.service';


const routes: Routes = [
  Test.childRoutes([
    {
      path: '',
      component: SimulatorPageComponent
    }
  ])
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
  declarations: []
})
export class SimulatorRoutingModule { }

  //Test.service
  static childRoutes(routes: Routes): Route {  
    return {
      path: '',
      component: TestComponent,
      children: routes,
      data: { reuse: true }
    };
  }

谁知道发生了什么?

【问题讨论】:

    标签: angular routes angular-load-children


    【解决方案1】:

    我像下面这样使用它。 Ofc,你可以用其他任何方式来做。

    文件夹结构:

    • /默认
    • /富
      • /子
    • /foo-bar

    src/app/app-routing.module.ts

    const routes: Routes = [
        {
            path: '', // localhost:4200/
            loadChildren: () => import('./default/default.module').then(m => m.DefaultModule), // lazy load
        },
        {
            path: 'foo', // localhost:4200/foo
            loadChildren: () => import('./foo/foo.module').then(m => m.FooModule), // lazy load
        },
        {
            path: 'foo/bar', // localhost:4200/foo/bar
            loadChildren: () => import('./foo-bar/foo-bar.module').then(m => m.FooBarModule), // lazy load
        },
        {
            path: 'baz', // localhost:4200/baz
            component: BazComponent,
        },
    ];
    
    @NgModule({
        imports: [RouterModule.forRoot(routes)],
        exports: [RouterModule]
    })
    export class AppRoutingModule {
    }
    

    app.component.html

    <header><!-- code --></header>
    <main>
        <router-outlet></router-outlet>
    </main>
    <footer><!-- code --></footer>
    

    因为我使用loadChildren,所以我需要一个组件加载。所以我在./default./foo./foo-bar 目录中需要更多“xy-routing.module.ts”:

    例如foo-routing.module.ts 包含在foo.module.ts 中的内容。

    const routes: Routes = [
        {
            path: '', // every time is ''. Ofc, you can use ':id' here -> when /foo/100   
            component: FooComponent
        }
    ];
    
    @NgModule({
        imports: [RouterModule.forChild(routes)],
        exports: [RouterModule]
    })
    export class FooRoutingModule {
    }
    

    如果你想在src/app/foo-bar/foo-bar.component.html中使用&lt;router-outlet&gt;&lt;/router-outlet&gt;

    src/app/foo-bar/foo-bar-routing.module.ts

    onst routes: Routes = [
        {
            path: '',
            component: FooBarComponent,
            children: [
                {
                    path: 'sub', // localhost:4200/foo/bar/sub
                    loadChildren: () => import('./sub/sub.module').then(m => m.SubModule) // src/app/foo-bar/sub/sub.module.ts
                },
            ]
        },
    ];
    
    @NgModule({
        imports: [RouterModule.forChild(routes)],
        exports: [RouterModule]
    })
    export class FooBarRoutingModule {
    }
    

    【讨论】:

    • #Numichi 感谢您的回答。这个是完美的,因为它们是所有模块的路由文件。但在我的情况下,TestModule 没有路由文件。但是只有一个组件(TestComponent)和模拟器组件是这个TestComponent的子组件。
    • 然后看BazComponent。 “baz”没有路由文件。
    • 但是“baz”是一个正常的组件。以“foo”模块(app.routing 的第二条路由)为例,假设 foo 模块是 foo-bar 模块的子模块,但 foo-bar 模块没有路由文件。你将如何进行?如果您能更好地理解我的问题,请再看我的帖子!
    • 你的想法应该可行。我在我的开发项目中尝试过它并且它可以工作,如果概念是 TestComponent 的“SimulatorPageComponent”内容。我不知道 test.component.html 中是否存在 &lt;router-outlet&gt;,其中加载 SimulatorPageComponent。
    【解决方案2】:

    非常感谢 Numichi 的帮助。最后它的工作。主要问题是它只显示一个空白页面,我不知道如何检查这些错误,所以在打开控制台并逐渐应用您的建议后它可以工作。

    【讨论】:

      猜你喜欢
      • 2018-06-17
      • 2020-02-25
      • 2019-10-17
      • 1970-01-01
      • 2019-05-30
      • 2014-05-22
      • 2017-03-30
      • 2020-05-24
      • 2020-01-15
      相关资源
      最近更新 更多