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

//相当于vue中

  • 一级路由
  • 路由重定向
  • 不匹配路由的处理(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. <ul>
  2. <li><a [routerLink]="['/home']" routerLinkActive="router-link-active">home</a></li>
  3. <li><a [routerLink]="['/news']" routerLinkActive="router-link-active">news</a></li>
  4. <li><a [routerLink]="['/about']" routerLinkActive="router-link-active">about</a></li>
  5. </ul>