【问题标题】:Nested Router Outlet With Name Not Working名称不工作的嵌套路由器插座
【发布时间】:2019-03-20 08:21:07
【问题描述】:

我正在为嵌套的路由器插座苦苦挣扎。我的情况是这样的:

我的 app.components.html 中有一个 <router-outlet>

然后我通过 url 延迟加载我的 desktop.module.ts 模块:/d

看起来像这样:

const routes: Routes = [
    { path: 'workout', component: WorkoutComponent, outlet: 'desktop'},
    { path: '', component: DesktopComponent, pathMatch: 'full' }
];


@NgModule({
    imports: [
        CommonModule,
        IonicModule,
        RouterModule.forChild(routes)
    ],
    declarations: [WorkoutComponent, DesktopComponent],
})
export class DesktopModule {}

现在在我的desktop.component.html 里面我有另一个这样的出口:

<router-outlet name="desktop"></router-outlet>

在桌面组件内部,我想使用“桌面”插座路由到我的WorkoutComponent。所以使用 url:/d/workout 我在 DesktopComponent 中显示我的 WorkoutComponent。

但我似乎无法前往锻炼路线。我试过这个:

[routerLink]="/d/workout, outlet: { "desktop" }"

routerLink="/d/(desktop:workout)"

但两者都不起作用,我找不到任何有效的答案。

【问题讨论】:

  • DesktopComponent 中显示WorkoutComponent 第一个应该是第二个的子级。命名的路由器插座不能与兄弟姐妹一起使用。所以换句话说,你需要创建一个名为 router-outlet 的父组件,然后使用 WorkoutComponent 作为子组件
  • @BotanMan 我尝试将锻炼路线设置为子路线,但我仍然无法导航到 d/workout 或 d/(desktop:workout) 之类的内容。不过感谢您的帮助!
  • @BotanMan 没关系,我刚刚找到了一个灵魂

标签: angular angular2-routing


【解决方案1】:

在 desktop.module.ts 中你需要导出 RouterModule

@NgModule({
    imports: [
        CommonModule,
        IonicModule,
        RouterModule.forChild(routes)
    ],
    declarations: [WorkoutComponent, DesktopComponent],
    exports: [RouterModule]
})

你应该像这样导航(试试你的方法,如果仍然不工作试试这个):

[routerLink]="['/d/workout']"
routerLink="/d/workout"

【讨论】:

  • 它不工作,我也不明白为什么如果我导出我的模块它应该工作,我不需要在其他地方使用我的模块。
  • 哦,它是根应用程序模块?好吧,我的错。因为文件名,我是这么认为的
  • 不用担心,我刚刚找到解决方案
  • 我找到了一个很棒的教程,我发现我不需要命名出口,当我设置子路线时,它会自动选择父母出口。这是教程:tektutorialshub.com/angular-child-routes-nested-routes
猜你喜欢
  • 2021-03-13
  • 2017-02-21
  • 2020-05-16
  • 1970-01-01
  • 2018-08-18
  • 2020-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多