1、用cli创建项目时添加路由

router.png

  1. 新增以下文件

i.png

  1. //app.component.html
  2. <router-outlet></router-outlet>
  3. 相当于<router-view></router-view>

2、创建组件

  1. ng g component components/组件名
  2. ng g component components/about

3、配置

一级路由、路由重定向、错误组件的处理

中文网址

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

4、路由导航

i.png

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

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>
  7. <router-outlet></router-outlet>
  1. //app.component.css
  2. .router-link-active{
  3. color:red;
  4. }

5、跳转

配置路由

  1. //app-routing.module.ts
  2. import { DetailComponent } from './components/detail/detail.component';
  3. const routes: Routes = [
  4. ...
  5. ,{
  6. path:'detail',
  7. component:DetailComponent
  8. },
  9. ...
  10. ];

不带参数跳转

  1. //home
  2. //.html
  3. <a [routerLink]="['/detail']">跳转detail</a>

携带参数跳转(get传值)

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

接收数据this.route.queryParams

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

6、动态路由

配置动态路由

  1. //app-routing.module.ts
  2. import { DetailComponent } from './components/detail/detail.component';
  3. const routes: Routes = [
  4. ...
  5. ,{
  6. path:'detail/:id',
  7. component:DetailComponent
  8. },
  9. ...
  10. ];

跳转传值

  1. //home
  2. //.ts
  3. export class HomeComponent implements OnInit {
  4. public id:string = "54561"
  5. constructor() { }
  6. ngOnInit() {
  7. }
  8. }

方法一

  1. //home
  2. //.html
  3. <a [routerLink]="['/detail',id]">跳转detail</a>

方法二

  1. <a routerLink="/detail/{{id}}">跳转detail</a>

接收this.route.params

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

7、事件跳转this.router.navigate

7-1、动态路由的事件跳转

  1. //home
  2. //.html
  3. <button (click)="handleClick()">跳转detail</button>
  1. //home
  2. //.ts
  3. import {Router} from "@angular/router"
  4. export class HomeComponent implements OnInit {
  5. public id:string = "54561"
  6. constructor(private router:Router) { }
  7. ngOnInit() {
  8. }
  9. handleClick(){
  10. this.router.navigate(["/detail",this.id])
  11. }
  12. }
  1. //detail
  2. //.ts
  3. import {ActivatedRoute} from '@angular/router'
  4. ...
  5. export class DetailComponent implements OnInit {
  6. constructor(public route:ActivatedRoute) { }
  7. ngOnInit() {
  8. this.route.queryParams.subscribe(res=>{
  9. console.log(res['id'])
  10. })
  11. }
  12. }

7-2、get传值的事件跳转

  1. //home
  2. //.ts
  3. //导入NavigationExtras
  4. import {Router,NavigationExtras} from "@angular/router"
  5. ...
  6. export class HomeComponent implements OnInit {
  7. constructor(private router:Router) { }
  8. ngOnInit() {
  9. }
  10. handleClick(){
  11. let navigationExtras:NavigationExtras = {
  12. queryParams:{
  13. "id":"12454"
  14. }
  15. }
  16. this.router.navigate(['/detail'],navigationExtras)
  17. }
  18. }
  1. //home
  2. //.ts
  3. import { Component, OnInit } from '@angular/core';
  4. import {Router,NavigationExtras} from "@angular/router"
  5. ...
  6. export class HomeComponent implements OnInit {
  7. constructor(private router:Router) { }
  8. public id:string = "15628"
  9. ngOnInit() {
  10. }
  11. handleClick(){
  12. let navigationExtras:NavigationExtras = {
  13. queryParams:{
  14. "id":this.id
  15. }
  16. }
  17. this.router.navigate(['/detail'],navigationExtras)
  18. }
  19. }
  1. //detail
  2. //.ts
  3. import {ActivatedRoute} from '@angular/router'
  4. ...
  5. export class DetailComponent implements OnInit {
  6. constructor(public route:ActivatedRoute) { }
  7. ngOnInit() {
  8. this.route.queryParams.subscribe(res=>{
  9. console.log(res['id'])
  10. })
  11. }
  12. }

9、子路由

配置子路由

  1. //app-routing.module.ts
  2. import { NgModule } from '@angular/core';
  3. import { Routes, RouterModule } from '@angular/router';
  4. ...
  5. import { MorningComponent } from './components/morning/morning.component'
  6. import { NightComponent } from './components/night/night.component'
  7. const routes: Routes = [
  8. ...
  9. ,{
  10. path:'news',
  11. component:NewsComponent,
  12. children:[
  13. {
  14. path:'morning',
  15. component:MorningComponent
  16. },{
  17. path:'night',
  18. component:NightComponent
  19. },{
  20. path:'',
  21. redirectTo:"morning",
  22. pathMatch:'full'
  23. }
  24. ]
  25. },
  26. ...
  27. ];
  28. ...
  1. //news
  2. //.html
  3. <p>news works!</p>
  4. <ul>
  5. <li><a [routerLink]="['/news/morning']" routerLinkActive="router-link-active">morning</a></li>
  6. <li><a [routerLink]="['/news/night']" routerLinkActive="router-link-active">night</a></li>
  7. </ul>
  8. <router-outlet></router-outlet>