this.router.navigate

5-1 动态路由的事件跳转

  1. <button (click)="handleClick()">跳转detail</button>
  1. //1.导入路由
  2. import {Router} from "@angular/router"
  3. export class HomeComponent implements OnInit {
  4. public id:string = "1213"
  5. //2.配置
  6. constructor(private router:Router) { }
  7. handleClick(){
  8. //3.跳转
  9. this.router.navigate(['/detail',this.id])
  10. }
  11. }

5-2get传值的事件跳转

**先将app-routing.module.ts中的动态路由写法去掉
  1. {
  2. path:"detail/:id",
  3. component:DetailComponent
  4. },
  5. 改成:
  6. {
  7. path:"detail",
  8. component:DetailComponent
  9. },
  1. //首页导入NavigationExtras
  2. import {Router,NavigationExtras} from "@angular/router"
  1. //配置 NavigationExtras的参数
  2. export class HomeComponent implements OnInit {
  3. public id:string = "1213"
  4. constructor(private router:Router) { }
  5. handleClick(){
  6. let navigationExtras:NavigationExtras ={
  7. queryParams:{
  8. "id":this.id
  9. }
  10. }
  11. //跳转
  12. this.router.navigate(['/detail'],navigationExtras)
  13. }
  14. }