1 首先angul-cli中配置的时候选yes,就是默认配置自己配置路由

2 配置好后,app.html中会有

微信截图_20191220092723.png

这个相当于vue中的router-view

3 配置一级路由

在app.routing中

  1. 引入
  2. import { NewsComponent } from './component/news/news.component';
  3. import { AboutComponent } from './component/about/about.component';
  4. import { HomeComponent } from './component/home/home.component';
  1. 配置
  2. {
  3. path:'home',
  4. component: HomeComponent
  5. },{
  6. path:'news',
  7. component:NewsComponent
  8. },{
  9. path:"about",
  10. component:AboutComponent
  11. }

3 输入地址404

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

效果 随意输入一个没有配置的地址

微信截图_20191220093415.png

4 路由重定向

  1. {
  2. path:"",
  3. redirectTo:"home",
  4. pathMatch:'full'
  5. }

5 routerLinkActive选中默认选中的路由

  1. <ul>
  2. <li><a [routerLink]="['/home']" routerLinkActive="router-link-active">home</a></li>
  3. <li> <a [routerLink]="['/about']" routerLinkActive="router-link-active">about</a></li>
  4. <li> <a [routerLink]="['/news']" routerLinkActive="router-link-active">news</a></li>
  5. </ul>
  6. css样式
  7. .router-link-active{
  8. color:red;
  9. }

6 跳转到详情页 get传值

跳转

  1. <a [routerLink]="['/detail']" [queryParams]='{id:id}' >跳转到detail</a>
  2. 首先要定义id 在主页面的ts
  3. export class HomeComponent implements OnInit {
  4. public id:string="123"
  5. constructor() {
  6. }

接收
detail.ts this.route.queryParams

  1. 导入
  2. import {ActivatedRoute} from '@angular/router'
  3. 构造函数中配置
  4. constructor(public route:ActivatedRoute) { }
  5. 使用
  6. ngOnInit() {
  7. this.route.queryParams.subscribe(res=>{
  8. console.log(res['id'])
  9. })
  10. }

7 动态路由

配置动态路由

  1. import { DetailComponent } from './component/detail/detail.component';
  2. {
  3. path:"detail/:id",
  4. component: DetailComponent
  5. },

跳转
第一种方式

  1. <a [routerLink]="['/detail',id]" >跳转到detail</a>

第二种方式

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

接收

  1. 导入
  2. import {ActivatedRoute} from '@angular/router'
  3. 构造函数中配置
  4. constructor(public route:ActivatedRoute) { }
  5. 使用
  6. ngOnInit() {
  7. this.route.params.subscribe(res=>{
  8. console.log(res['id'])
  9. })
  10. }

8 动态路由的事件跳转

this.router.navigate([‘/detail’])

  1. <button (click)="handleClick()">跳转到detail</button>
  2. 导入
  3. import {Router} from '@angular/router'
  4. 构造函数中配置
  5. constructor(private router:Router) {
  6. }
  7. 使用
  8. handleClick(){
  9. this.router.navigate(['/detail'])
  10. }
  11. id
  12. handleClick(){
  13. this.router.navigate(['/detail',this.id])
  14. }

接收

  1. 导入
  2. import {ActivatedRoute} from '@angular/router'
  3. 构造函数中配置
  4. constructor(public route:ActivatedRoute) { }
  5. 使用
  6. ngOnInit() {
  7. this.route.params.subscribe(res=>{
  8. console.log(res['id'])
  9. })
  10. }

9 get传值的事件跳转

  1. 导入
  2. import {Router,NavigationExtras} from '@angular/router'
  3. 构造函数中配置
  4. constructor(private router:Router) {
  5. }
  6. 使用
  7. handleClick(){
  8. let navigationExtras:NavigationExtras={
  9. queryParams:{
  10. "id":"1234"
  11. }
  12. }
  13. this.router.navigate(['/detail'],navigationExtras)
  14. }
  15. 传不固定的值
  16. export class HomeComponent implements OnInit {
  17. public id:string="123"
  18. constructor(private router:Router) {
  19. }
  20. handleClick(){
  21. let navigationExtras:NavigationExtras={
  22. queryParams:{
  23. "id":this.id tips
  24. }
  25. }
  26. this.router.navigate(['/detail'],navigationExtras)
  27. }

接收

  1. 导入
  2. import {ActivatedRoute} from '@angular/router'
  3. 构造函数中配置
  4. constructor(public route:ActivatedRoute) { }
  5. ngOnInit() {
  6. this.route.queryParams.subscribe(res=>{
  7. console.log(res['id'])
  8. })
  9. }

子路由

  1. import { MorningComponent } from './component/morning/morning.component';
  2. import { NightComponent } from './component/night/night.component';
  3. 配置
  4. {
  5. path:'news',
  6. component:NewsComponent ,
  7. children:[
  8. {
  9. path:"morning",
  10. component:MorningComponent
  11. },{
  12. path:'night',
  13. component: NightComponent
  14. },{
  15. path:"",
  16. redirectTo:"morning",
  17. pathMatch:"full"
  18. }
  19. // 路由重定向
  20. ]
  21. },
  22. tips
  23. # <router-outlet></router-outlet> 一定要在父路由对应的组件中的html中加这个