【问题标题】:Relative links/partial routes with routerLink使用 routerLink 的相对链接/部分路由
【发布时间】:2016-11-07 19:51:46
【问题描述】:

在我的应用中,一些 URL 采用这种形式

/department/:dep/employee/:emp/contacts

在我的侧边栏中,我显示了所有员工的列表,每个员工都有一个 [routerLink] 链接到该员工的联系人

<ul>
    <li>
        <a [routerLink]="['/department', 1, 'employee', 1, 'contacts']"></a>
    <li>
    <li>
        <a [routerLink]="['/department', 1, 'employee', 2, 'contacts']"></a>
    <li>
    ...
</ul>

但是,使用这种方法,我总是需要提供完整的路径,包括所有参数(如上面示例中的 dep),这非常麻烦。有没有办法只提供部分路线,比如

<a [routerLink]="['employee', 2, 'contacts']"></a>

(没有 department 部分,因为我并不真正关心 department 在这种观点中,而且我的感觉是这无论如何都违背了关注点分离)?

【问题讨论】:

标签: angular angular2-routing


【解决方案1】:

应该可以的。前导的/ 使其成为绝对路线,没有它(或使用./),它将成为相对于当前路线的相对路线。您还可以使用../(或../../ 或更多)来相对于父级或父级父级进行路由。

【讨论】:

  • ./ 和没有它都不会做相对的routerLinks 工作。它们在页面加载后工作一次,但如果父组件更改其 URL 部分,则为具有相对 routerLink 的组件生成的 href 不会更新并保持在先前的路由上。在github.com/erasethis/angular2-routing查看我的仓库@
  • 我猜这需要 Plunker 来重现和调查angular.io/resources/live-examples/quickstart/ts/plnkr.html 你有一个&lt;base href="/"&gt; 作为&lt;head&gt; 中的第一个元素吗?
  • 我认为这与定义routerLink 的组件的路由有关。如果我激活了/parent/child/grandchild1 路由并且routerLink/parent 组件(其中包含OP 的侧边栏)中定义,那么指向grandchild2 的链接仍会期望routerLink 包含child,例如。 [routerLink]="[child, grandchild2]".
  • 可以确认。 ./ 工作并加载当前路由的子组件。
  • @pbarranis “它根本行不通”——没有更多细节我无法帮助你。如果您的问题与 OP 不同,请尝试在 stackblitz 中重现并发布您自己的问题
【解决方案2】:

我建议在 router.navigate 函数中使用 relativeTo 参数,而不是使用 './' 或 '../'。例如:

this.router.navigate(['child'], {relativeTo: this.route});

this.router.navigate(['sibling'], {relativeTo: this.route.parent});

【讨论】:

    【解决方案3】:

    如果您的 app-routing.module.ts 看起来与此类似:

      {
        path: '/department/:dep', component: DepartmentComponent, children: [
          { path: 'employee/:emp/contacts', component: EmployeeComponent },
          // ... Other child routes
        ]
      }
    

    假设您在父路由http://localhost:4200/department/1 上,现在下面的routerLinks 将按上述方式工作:

    1. 前缀./表示相对路径从当前路线
    2. 无前缀表示相对路径与当前路线
    3. 前缀/代表绝对路径从根路由
    4. 前缀../ 表示相对路径从当前路线上移一级
    <ul>
    
      <!-- will generate URL: http://localhost:4200/department/1/employee/1/contacts-->
      <li class="nav-item">
         <a class="nav-link" routerLink="recipes" routerLink="./employee/1/contacts">Relative Link that Works!:)</a>
      </li>
    
      <!-- is same as above and generated URL: http://localhost:4200/department/1/employee/1/contacts-->
      <li class="nav-item">
         <a class="nav-link" routerLink="recipes" routerLink="employee/1/contacts">Relative Link that Works!:)</a>
      </li>
    
      <!-- is same as above and generated URL: http://localhost:4200/department/1/employee/1/contacts-->
      <li class="nav-item">
         <a class="nav-link" routerLink="recipes" [routerLink]="['employee', '1', 'contacts']">Relative Link that Works! :)</a>
      </li>
    
      <!-- is NOT THE SAME as above  and generated URL: http://localhost:4200/department/employee/1/contacts-->
      <li class="nav-item">
         <a class="nav-link" routerLink="recipes" routerLink="../employee/1/contacts">Relative Link that does NOT work! :( </a>
      </li>
    
      <!-- is NOT THE SAME as above  and generated URL: http://localhost:4200/employee/1/contacts-->
      <li class="nav-item">
         <a class="nav-link" routerLink="recipes" routerLink="/employee/1/contacts">Absolute Link that does NOT work! :( </a>
      </li>
    
    </ul>
    

    【讨论】:

      【解决方案4】:

      正如官方 Angular 文档 here 中所述 查看

      <ul>
          <li>
              <a (click)="onViewDetails(id)">Details</a>
          <li>
          ...
      

      组件

      construct( private router: Router, private route: ActivatedRoute ){
      }
      onViewDetails(id){
       this.router.navigate([id], {relativeTo: this.route});
      }
      

      /雇员
      /雇员/:id

      【讨论】:

      • @adamdport 您指的是哪个版本的 Angular?自从我的回答以来,Angular 发生了很大变化:D 你可以查看官方文档以获取有关路由器的更多信息..thnx
      • @user776686 Angular 作为 SPA,它没有“在新选项卡中打开”的“方式”,但您可以使用 JS 的 window 对象轻松实现。检查此代码:``` let newRelativeUrl = router.createUrlTree([id], {relativeTo: this.route});让 baseUrl = window.location.href.replace(router.url, ''); window.open(baseUrl + newRelativeUrl, '_blank'); ```我没有测试它,但理论上应该可以工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-23
      • 2017-05-27
      • 2018-01-03
      • 2015-06-04
      • 2010-10-06
      • 2019-10-27
      相关资源
      最近更新 更多