官网https://angular.cn/guide/router

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

    //相当于vue中

    1. <a name="8L5N9"></a>
    2. ### 1.一级路由
    3. - 一级路由
    4. - 路由重定向
    5. - 不匹配路由的处理(404)
    6. 新建四个组件home/about/news/error
    7. <a name="1QWA4"></a>
    8. #### app-routing.module.ts中配置
    9. - **路由重定向和错误处理要放在最后面,不然会出错**
    10. ```javascript
    11. import { Routes, RouterModule } from '@angular/router';
    12. import { HomeComponent } from './components/home/home.component';
    13. import { NewsComponent } from './components/news/news.component';
    14. import { AboutComponent } from './components/about/about.component';
    15. import { ErrorComponent } from './components/error/error.component';
    16. const routes: Routes = [
    17. {
    18. path:'home',
    19. component:HomeComponent
    20. },{
    21. path:'news',
    22. component:NewsComponent
    23. },{
    24. path:"about",
    25. component:AboutComponent
    26. },{
    27. //路由重定向
    28. path:"",
    29. redirectTo:"home",
    30. pathMatch:"full" //严格匹配 一定要加
    31. },{
    32. // 匹配没有设置的路由
    33. path:"**",
    34. component:ErrorComponent
    35. }
    36. ];
    37. ...
    38. export class AppRoutingModule { }