https://angular.cn/guide/router

路由

angular-cli中选中路由,之后app.component.html会多出

  1. <router-outlet></router-outlet>
  2. //相当于vue中
  3. <router-view></router-view>

一、一级路由

  1. - 一级路由
  2. - 路由重定向
  3. - 不匹配路由的处理(404)

app-routing.module.ts中配置

  1. import { Routes, RouterModule } from '@angular/router';
  2. import { HomeComponent } from './components/home/home.component';
  3. import { NewsComponent } from './components/news/news.component';
  4. import { AboutComponent } from './components/about/about.component';
  5. import { ErrorComponent } from './components/error/error.component';
  6. const routes: Routes = [
  7. {
  8. path:'home',
  9. component:HomeComponent
  10. },{
  11. path:'news',
  12. component:NewsComponent
  13. },{
  14. path:"about",
  15. component:AboutComponent
  16. },{
  17. //路由重定向
  18. path:"",
  19. redirectTo:"home",
  20. pathMatch:"full"
  21. },{
  22. // 匹配没有设置的路由
  23. path:"**",
  24. component:ErrorComponent
  25. }
  26. ];
  27. ...
  28. export class AppRoutingModule { }

二、routerLinkActive设置routerLink默认选中的路由

  1. //app.component.html
  2. <ul>
  3. <li><a [routerLink]="['/home']" routerLinkActive="router-link-active">home</a></li>
  4. <li><a [routerLink]="['/news']" routerLinkActive="router-link-active">news</a></li>
  5. <li><a [routerLink]="['/about']" routerLinkActive="router-link-active">about</a></li>
  6. </ul>

三、get传值

3-1 跳转

  1. <a [routerLink]="['/detail']" [queryParams]="{id:id}">跳转detail</a>

3-2 接收 this.route.queryParams

  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. }

四、动态路由

4-1 配置

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

4-2 跳转传值

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

4-3 接收 this.route.params

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

五、事件跳转

this.router.navigate

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

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

5-2 get传值的事件跳转

//导入NavigationExtras
import {Router,NavigationExtras} from "@angular/router"

//配置 NavigationExtras的参数
export class HomeComponent implements OnInit {
  public id:string = "1213"
  constructor(private router:Router) { }
  handleClick(){
    let navigationExtras:NavigationExtras ={
      queryParams:{
        "id":this.id
      }
    }
    //跳转
    this.router.navigate(['/detail'],navigationExtras)
  }
}

六、子路由

{
    path:'news',
    component:NewsComponent,
    children:[
      {
        path:"morning",
        component:MorningComponent
      },{
        path:"night",
        component:NightComponent
      },{
        path:"",
        redirectTo:"morning",
        pathMatch:"full"
      }
    ]
  }

//在父路由对应的组件中的html中
<router-outlet></router-outlet>