【问题标题】:How do I navigate to a sibling route?如何导航到同级路线?
【发布时间】:2016-12-02 17:16:57
【问题描述】:

假设我得到了这个路由器配置

export const EmployeeRoutes = [
   { path: 'sales', component: SalesComponent },
   { path: 'contacts', component: ContactsComponent }
];

并已通过此 URL 导航到 SalesComponent

/department/7/employees/45/sales

现在我想去contacts,但由于我没有绝对路线的所有参数(例如,部门ID,在上面的例子中7)我更愿意去那里使用相对链接,例如

[routerLink]="['../contacts']"

this.router.navigate('../contacts')

不幸的是,这不起作用。可能有一个明显的解决方案,但我没有看到它。任何人都可以在这里帮忙吗?

【问题讨论】:

    标签: angular angular2-routing angular2-router


    【解决方案1】:

    如果您使用的是新路由器(3.0.0-beta2),您可以使用 ActivatedRoute 导航到相对路径,如下所示:

    constructor(private router: Router, private r:ActivatedRoute) {} 
    
    ///
    // DOES NOT WORK SEE UPDATE
    goToContact() {
      this.router.navigate(["../contacts"], { relativeTo: this.r });
    }
    

    2019 年 8 月 2 日更新 Angular 7.1.0

    current route: /department/7/employees/45/sales

    the old version will do: /department/7/employees/45/sales/contacts

    根据@KCarnaille 的评论,以上不适用于最新的路由器。新的方式是把.parent加到this.r所以

        // Working(08/02/2019) 
        // DOES NOT WORK SEE UPDATE
        goToContact() {
           this.router.navigate(["../contacts"], { relativeTo: this.r.parent });
        }
    

    the update will do: /department/7/employees/45/contacts

    2021 年 12 月 12 日更新 Angular > 10

    正如@ziz194 提到的,这就是它现在的工作方式。

    constructor(private router: Router, private r:ActivatedRoute) {} 
    
    goToContact() {
      this.router.navigate(["contacts"], { relativeTo: this.r });
    }
    

    【讨论】:

    • 我其实也遇到了同样的情况,但对我来说不起作用……自正式发布以来发生了什么变化?
    • @KCarnaille 嗯,我正在使用@angular/router 3.0.0(已完成,不是测试版),它仍然可以正常工作。不知道从那以后他们是否有任何重大变化。
    • 我们如何使用routerLink从html做到这一点?
    • 这实际上不应该由 .parent 解决,只需删除 ../ 并在路径中保留“联系人”,如下所示:this.router.navigate(["contacts"], { relativeTo: this.r});
    • 我真的不明白这个逻辑。所以相对于父路由,我们向后导航一级(祖父),然后前进到联系人。这应该给我们/department/7/employees/contacts
    【解决方案2】:

    RouterLink 指令始终将提供的链接视为当前 URL 的增量:

    [routerLink]="['/absolute']"
    [routerLink]="['../../parent']"
    [routerLink]="['../sibling']"
    [routerLink]="['./child']"     // or
    [routerLink]="['child']" 
    
    // with route param     ../sibling;abc=xyz
    [routerLink]="['../sibling', {abc: 'xyz'}]"
    // with query param and fragment   ../sibling?p1=value1&p2=v2#frag
    [routerLink]="['../sibling']" [queryParams]="{p1: 'value', p2: 'v2'}" fragment="frag"
    

    navigate() 方法需要一个起点(即relativeTo 参数)。如果没有提供,导航是绝对的:

    constructor(private router: Router, private route: ActivatedRoute) {}
    
    this.router.navigate(["/absolute/path"]);
    this.router.navigate(["../../parent"], {relativeTo: this.route});
    this.router.navigate(["../sibling"],   {relativeTo: this.route});
    this.router.navigate(["./child"],      {relativeTo: this.route}); // or
    this.router.navigate(["child"],        {relativeTo: this.route});
    
    // with route param     ../sibling;abc=xyz
    this.router.navigate(["../sibling", {abc: 'xyz'}], {relativeTo: this.route});
    // with query param and fragment   ../sibling?p1=value1&p2=v2#frag
    this.router.navigate(["../sibling"], {relativeTo: this.route, 
        queryParams: {p1: 'value', p2: 'v2'}, fragment: 'frag'});
    
    // RC.5+: navigate without updating the URL 
    this.router.navigate(["../sibling"], {relativeTo: this.route, skipLocationChange: true});
    

    【讨论】:

    • 为什么导航的行为不同?
    • 您是否有使用同级路由的 [routerLink] 指令的工作示例?如果我使用 [routerLink]="['../sibling']",它会一直为我切换到绝对路由
    • router.navigate() 要求第一个参数是一个数组 - 但这些都不能作为数组工作
    • @Jonathan002,我也一样。你找到什么有用的了吗?
    • 这个答案并不完全准确。路径 ../somethingrelativeTo,其中 component 在路由器树中呈现,但它与 current route 无关,因为这可能成为应用[routerLink] 的路由树中更深的路由。如果您尝试在页面标题中使用面包屑作为示例。它将与该标题的呈现位置相关,并且很可能不是您想要的。
    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2020-10-16
    • 2021-10-11
    • 2018-02-15
    • 2016-11-02
    相关资源
    最近更新 更多