3-1跳转

首页中跳转 home.component.html
  1. <a [routerLink]="['/detail']" [queryParams]="{id:id}">跳转到detail</a>

id是个变量,要先在home.component.ts中声明
  1. export class HomeComponent implements OnInit {
  2. public id:string="123"
  3. ...
  4. }

3-2详情页接收 this.route.queryParams

detail.component.ts
  1. //导入正在显示的路由
  2. import {ActivatedRoute} from '@angular/router'
  3. export class DetailComponent implements OnInit {
  4. //构造函数中配置
  5. constructor(public route:ActivatedRoute) { }
  6. ngOnInit() {
  7. //subscribe触发
  8. this.route.queryParams.subscribe(res=>{
  9. console.log(res)
  10. })
  11. }
  12. }