简体   繁体   English

Angular2子路由

[英]Angular2 Child Routing

i have some problem. 我有一些问题。 let me explain 让我解释

this is my html code 这是我的HTML代码

<div class="alert alert-danger alert-dismissable" *ngIf="errorMessage">
   <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
   <strong>{{ errorMessage }}</strong>
</div>
<h3 class="page-header">All Unread Report</h3>
<table class="table table-hover">
  <thead>
    <tr>
       <th>NIK</th>
       <th>Nama</th>
       <th>Topik</th>
       <th>Detail</th>
    </tr>
   </thead>
   <tbody>
    <tr *ngFor="let item of allcomplaint;let i= index">
      <td>{{i+1}}</td>
      <td>{{item.sendername}}</td>
      <td>{{item.topic}}</td>
      <td>
        <a class="btn btn-success" routerLink="/edit/{{item.id}}">Edit</a>
      </td>
    </tr>
 </tbody>
</table>

what i want is if i click that edit button link it will navigate to other component. 我想要的是,如果我单击该编辑按钮链接,它将导航到其他组件。 but i always get error like this below 但是我总是在下面收到这样的错误

Uncaught (in promise): Error: Cannot match any routes. 未捕获(承诺):错误:无法匹配任何路由。 URL Segment: 'edit/1' Error: Cannot match any routes. URL段:'edit / 1'错误:无法匹配任何路由。 URL Segment: 'edit/1 网址段:“ edit / 1

this is my route config 这是我的路线配置

{ path: 'dashboard_pool', component: DashboardPoolComponent,
  children:[
    { path: 'unread', component: PoolUnreadReportComponent,
      children:[
        {path:'detail/:id', component:PoolComplaintDetailComponent}
      ]
    },

how to resolve this? 该如何解决呢? thanks :) 谢谢 :)

It's because using routerLink like that calls for a relative path . 这是因为使用routerLink那样的routerLink需要相对路径

If you want to go to the child, use 如果您想去孩子那里,请使用

<a class="btn btn-success" [routerLink]="'edit/' + item.id">Edit</a>

Try with: 尝试:

// inside PoolUnreadReportComponent.html
<a class="btn btn-success" [routerLink]="['edit',item.id]">Edit</a>

// inside DashboardComponent.html
<a class="btn btn-success" [routerLink]="['unread/edit',item.id]">Edit</a>

Avoid using manual concatenation of parameters and use an array to pass the values instead (the so called link parameters array ). 避免使用手动连接参数,而使用数组来传递值(所谓的链接参数array )。 Angular takes care of the rest. Angular负责其余的工作。

Some extra info about routing: 有关路由的一些额外信息:

  • ../ means that you go one level up in the route tree ../表示您在路由树中上一层
  • ./ means sybling ./表示sybling
  • / means absolut path, [routerLink]="['/edit', item.id]" would be root/edit/:id for example /表示绝对路径,例如[routerLink]="['/edit', item.id]"将是root / edit /:id

For more info check the routing docs 有关更多信息,请查看路由文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM