【问题标题】:Cannot Navigate to Nested/Child Route using parent named router outlet无法使用父命名路由器插座导航到嵌套/子路由
【发布时间】:2023-04-07 02:08:02
【问题描述】:

我之前在 AngularJS 中使用 ui-router,然后在 Angular 2+ 中使用。我想转移到 @angular/router 只是因为它似乎受到更多支持并且在其上构建了更多库(ui-router-ng2 仍处于测试阶段!!)

我的问题是我正在使用一个已命名路由出口的主模板,因为主模板有一个可重用的静态页眉和页脚。 (还有一个侧边栏,我为了简洁起见)

我的问题是,一旦我进入我的“/mainDashboard”路由器,我无法从该路由导航到下一个嵌套/子路由“/otherDashboard”,同时尝试在主模板“内容”中使用命名路由器插座'

我应该如何在下面的代码第十三行导航到我的路线。 还有一个plunker https://plnkr.co/edit/RErIiby535x0toaA02W4?p=preview

这是我的代码

import { NgModule, Component } from '@angular/core';
import { RouterModule, Router } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';

@Component({
  selector: 'app-home',
  template: '<button (click)="go()">Go To Other Dashboard</button>'
})
export class HomeComponent {
  constructor( public router: Router ) {}
  go(){ 
    //THIS ERRORS
    this.router.navigate(['../dashboard',{outlets: {'content': ['../dashboard/mainDashboard/otherDashboard']}}]);
  }
}

@Component({
  selector: 'app-main-dashboard',
  template: `
    <header>My Static Header</header>
    <div id="main-container" class="main-background">
        <router-outlet name="content"></router-outlet>
    </div>
    <footer>My Static Footer</footer>
  `
})
export class MainDashboardComponent {
  constructor() {}
}

@Component({selector: 'app-login',template: '<button (click)="login()">Login</button>'})
export class LoginComponent {
    constructor(public router: Router) { };
    login(){
        this.router.navigate(['/dashboard',{outlets: {content: ['mainDashboard']}}]);
    }
}

@Component({selector: 'app-other-dashboard', template: '<h1>Hello Other Dashboard</h1>'})
export class OtherDashboardComponent{
  constructor() { }
}

export const routes: Array<any> = [
    {path: '',component: LoginComponent },
    {path: 'dashboard', component: MainDashboardComponent,
        children: [
            {path: 'mainDashboard',component: HomeComponent,outlet: 'content',
                children: [{path: 'otherDashboard',component: OtherDashboardComponent,  outlet: 'content'}]
            }
        ]
    }
]

    @Component({ selector: 'app-root', template: '<router-outlet></router-outlet>'})
    export class AppComponent {}

    @NgModule({
      declarations: [ AppComponent, LoginComponent, MainDashboardComponent, HomeComponent, OtherDashboardComponent],
      imports: [ BrowserModule, RouterModule.forRoot(routes) ],
      providers: [],
      bootstrap: [AppComponent],
    })
    export class AppModule { }

【问题讨论】:

    标签: angular


    【解决方案1】:

    在使用您的 plunker 代码后,我发现了问题所在。由于您的otherDashboardmainDashboard 的子级,因此mainDashboard 内部应该有otherDashboard 的路由器插座。添加后,我可以使用router.navigateByUrl 导航到otherDashboard 所以你的HomeComponent 看起来像:

    @Component({
      selector: 'app-home',
      template: '<button (click)="go()">Go To Other Dashboard</button><router-outlet name="content"></router-outlet>'
    })
    export class HomeComponent {
      constructor( public router: Router ) {}
      go(){ 
        //Error gone
        this.router.navigateByUrl('/dashboard/(content:mainDashboard/(content:otherDashboard))');
      }
    }
    

    如果您想将MainDashboardComponent 本身中的HomeComponent 替换为OtherDashboardComponent,那么otherDashboardmainDashboard 应该是HomeComponent 的同一父级的子级

    工作代码:https://plnkr.co/edit/jYOMNhbE3lX5UYmjS7qy?p=preview

    【讨论】:

    • 这种解决方案很糟糕,这解决不了任何问题。您只需输入路由器的确切网址。这不是抽象的,不能在任何其他情况下使用。每次您决定更改路径时,都必须在此处更改 URL...
    猜你喜欢
    • 2019-11-05
    • 2021-03-05
    • 2022-01-11
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2020-01-13
    • 2018-09-23
    相关资源
    最近更新 更多