一、配置

Tip: 首先创建项目时得选中路由

之后app.component.html会多出

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

app.routing.module.js配置

  1. import { HomeComponent } from './components/home/home.component';
  2. import { NewsComponent } from './components/news/news.component';
  3. const routes: Routes = [
  4. {
  5. path:'home',
  6. component:HomeComponent
  7. },
  8. {
  9. path:'news',
  10. component:NewsComponent
  11. }
  12. ]

二、路由重定向

  1. {
  2. path:'',
  3. redirectTo:'home',
  4. pathMatch:'full'
  5. //严格匹配
  6. }

三、匹配错误路由处理

  1. {
  2. //匹配没有设置的路由
  3. path:'**',
  4. component:ErrorComponent
  5. }

四、路由导航

  1. <ul>
  2. //也可写成<li><a [routerLink]="['/home']" routerLinkActive="router-link-active">home</a></li>
  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>
  7. <router-outlet></router-outlet>

routerLinkActive
当url是当前的时候,a标签将会被加上router-link-active。当url变化为别的时,class将会被移除。
也可以给routerLinkActive进行配置参数

  1. <a routerLink="/user/login" routerLinkActive="red">login</a>
  2. urluser或者/user/login的时候,a标签将会被加上classred。当url变化为别的时,class将会被移除。
  3. 如何添加两个class
  4. <a routerLink="/user/login" routerLinkActive="class1 class2">login</a>
  5. 也可以给routerLinkActive进行配置参数
  6. 传递exact: true表示路由完全匹配时才高亮,如
  7. <a routerLink="/user/login" routerLinkActive="red"[routerLinkActiveOptions]="{exact: true}"
  8. >login</a>

五、路由跳转传值

5-1 get传值

home页

  1. <a [routerLink]="['/detail']" [queryParams]="{id:id}">跳转detail</a>
  2. export class HomeComponent implements OnInit {
  3. public id:string = "123"
  4. constructor() { }
  5. ngOnInit() {
  6. }
  7. }

detail页接收
image.png

  1. import { Component, OnInit } from '@angular/core';
  2. //导入正在显示的路由
  3. import {ActivatedRoute} from '@angular/router'
  4. @Component({
  5. selector: 'app-detail',
  6. templateUrl: './detail.component.html',
  7. styleUrls: ['./detail.component.css']
  8. })
  9. export class DetailComponent implements OnInit {
  10. constructor(public route:ActivatedRoute) { }
  11. ngOnInit() {
  12. this.route.queryParams.subscribe(res=>{
  13. console.log(res)
  14. })
  15. }
  16. }

5-2 动态路由

路由配置

  1. {
  2. path:'detail/:id',
  3. component:DetailComponent
  4. }

home页

  1. //第一种跳转
  2. <a [routerLink]="['/detail',id]" >跳转detail</a>
  3. export class HomeComponent implements OnInit {
  4. public id:string = "123"
  5. constructor() { }
  6. ngOnInit() {
  7. }
  8. }
  1. //第二种
  2. <a routerLink="/detail/{{id}}" >跳转detail</a>

detail页接收

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

六、事件跳转传值

  1. {
  2. path:'detail/:id',
  3. component:DetailComponent
  4. }

home页

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

detail页接收

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

6-2 get事件传值

home页
Tip: 另导入NavigationExtras模块

  1. import {Router,NavigationExtras} from "@angular/router"
  2. ...
  3. export class HomeComponent implements OnInit {
  4. public id:string = "123"
  5. constructor(private router:Router) { }
  6. ngOnInit() {
  7. }
  8. handleClick(){
  9. let navigationExtras:NavigationExtras = {
  10. queryParams:{
  11. "id":this.id
  12. }
  13. }
  14. this.router.navigate(['/detail'],navigationExtras)
  15. }
  16. }

detail页接收

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

七、二级路由

  1. {
  2. path:'news',
  3. component:NewsComponent,
  4. children:[
  5. {
  6. path:"morning",
  7. component:MorningComponent
  8. },
  9. {
  10. path:"night",
  11. component:NightComponent
  12. },
  13. // 路由重定向
  14. {
  15. path:"",
  16. redirectTo:"morning",
  17. pathMatch:"full"
  18. }
  19. ]
  20. }

Tip:在一级路由对应的组件中的html中配置

  1. <router-outlet></router-outlet>