3 ways to pass parameters in Angular5 routing

  There are 3 methods in total.

  1. The parameters after the question mark, the way to get the parameters: ActivatedRoute.queryParams[id]

        For example: /product?id=1&name=iphone can also be: [ routerLink]= "['/books']" [ queryParams]= "{bookname: '"Alive"'}

code: html

<h4>Messages</h4>
<p>Total:{{msgs.total}}</p>
<div *ngFor="let item of msgs.data">
  <b>{{item.name}}</b>:
  <a [routerLink]="['/next',item.id]"  [queryParams]="{id:item.id, msg:item.msg, name:item.name}">{{item.msg}}</a>
  <a routerLink="/final">Reply</a><p></p>
</div>

  Get parameter js

by OnInit () {
    let obj = this.route.queryParams["_value"];
    console.log(obj);
}

  2. Colon form,

        For example: path:/product/:id

        How to get parameters: ActivatedRoute.params[id]

The first routelink in the html code above is.

Also need to configure routing

{
    path:'listDetail/:id',
    component:ListDetailComponent
  }
参考https://segmentfault.com/a/1190000012268186

  3. Path jump in js

        例如:path:/product,component:ProductComponent,data:[{madeInChina:true}]} 

        How to get parameters: ActivatedRoute.snapshot.data[0][madeInChina]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324985258&siteId=291194637