4-1配置

  1. //app-routing.module.ts
  2. {
  3. path:"detail/:id",
  4. component:DetailComponent
  5. }

id是个变量,要先在home.component.ts中声明

  1. export class HomeComponent implements OnInit {
  2. public id:string="123"
  3. ...
  4. }

4-2跳转传值

  1. //第一种跳转方式
  2. <a [routerLink]="['/detail',id]">跳转到detail</a>
  1. //第二种跳转方式
  2. <a routerLink="/detail/{{id}}">跳转到detail</a>

4-3接收this.route.params

  1. import {ActivatedRoute} from '@angular/router'
  2. export class DetailComponent implements OnInit {
  3. constructor(public route:ActivatedRoute) { }
  4. ngOnInit() {
  5. this.route.params.subscribe(res=>{
  6. console.log(res)
  7. })
  8. }
  9. }