Angular 5.x 學(xué)習(xí)筆記之Router(路由)應(yīng)用
序言:
Angular APP 視圖之間的跳轉(zhuǎn),依賴(lài)于 Router (路由),這一章,我們來(lái)講述 Router 的應(yīng)用
實(shí)例講解
運(yùn)行結(jié)果如下。 設(shè)置了3個(gè)導(dǎo)航欄, Home、 About、Dashboard。 點(diǎn)擊不同的導(dǎo)航欄,跳轉(zhuǎn)到相應(yīng)的頁(yè)面:

創(chuàng)建3個(gè) component
- ng g c home
- ng g c about
- ng g c dashboard
路由與配置
(1)**引入 Angular Router **
當(dāng)用到 Angular Router 時(shí),需要引入 RouterModule,如下:
// app.module.ts
import { RouterModule } from '@angular/router';
imports: [
BrowserModule, RouterModule
],
(2) 路由配置
還記得由誰(shuí)來(lái)管理component 的吧,沒(méi)錯(cuò),由 module 來(lái)管理。 所以,把新創(chuàng)建的 component,引入到 app.moudle 中。 如下:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { appRoutes } from './routerConfig';
import { AppComponent } from './app.component';
import { AboutComponent } from './components/about/about.component';
import { HomeComponent } from './components/home/home.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
提示: 注意component的路徑,為便于管理,我們把新創(chuàng)建的component 移到了 components 文件夾中。
創(chuàng)建 Router Configure 文件
在 app 目錄下, 創(chuàng)建 routerConfig.ts 文件。 代碼如下:
import { Routes } from '@angular/router';
import { HomeComponent } from './components/home/home.component';
import { AboutComponent } from './components/about/about.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
export const appRoutes: Routes = [
{ path: 'home',
component: HomeComponent
},
{
path: 'about',
component: AboutComponent
},
{ path: 'dashboard',
component: DashboardComponent
}
];
說(shuō)明: Angular 2.X 以上版本,開(kāi)始使用 TypeScript 編寫(xiě)代碼,而不再是 JavaScript,所以,文件的后綴是: ts 而不是 js
這個(gè) routerConfigue 文件,怎么調(diào)用呢? 需要把它加載到 app.module.ts 中,這是因?yàn)?app.moudle.ts 是整個(gè)Angular App 的入口。
// app.module.ts
import { appRoutes } from './routerConfig';
imports: [
BrowserModule,
RouterModule.forRoot(appRoutes)
],
聲明 Router Outlet
在 app.component.html 文件中,添加代碼:
<div style="text-align:center">
<h1>
{{title}}!!
</h1>
<nav>
<a routerLink="home" routerLinkActive="active">Home</a>
<a routerLink="about">About</a>
<a routerLink="dashboard">Dashboard</a>
</nav>
<router-outlet></router-outlet>
</div>
運(yùn)行
進(jìn)入到該工程所在的路徑, 運(yùn)行;
ng serve --open
當(dāng) webpack 編譯成功后,在瀏覽器地址欄中,輸入: http://localhost:4200
即可看到本篇開(kāi)始的結(jié)果。
關(guān)于Router,換一種寫(xiě)法:
在 app.moudle.ts 文件中,代碼如下 :
imports: [
BrowserModule,
RouterModule.forRoot(
[
{ path: 'home',
component: HomeComponent
},
{
path: 'about',
component: AboutComponent
},
{
path: 'dashboard',
component: DashboardComponent
}
]
)
],
這樣一來(lái),可以不用單獨(dú)創(chuàng)建 routerConfigure.ts 文件。
小結(jié)
自從引入了面向組件(component)后,路由管理相比 AngularJS (1.X),方便了很多。
進(jìn)一步優(yōu)化:
或許你已經(jīng)注意到,當(dāng)訪(fǎng)問(wèn) http://localhost:4200 時(shí),它的路徑應(yīng)該是 “/”, 我們應(yīng)該設(shè)置這個(gè)默認(rèn)的路徑。
{
path: '',
redirectTo:'/home',
pathMatch: 'full'
},
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Angularjs中UI Router全攻略
- angularJS中router的使用指南
- angular2中router路由跳轉(zhuǎn)navigate的使用與刷新頁(yè)面問(wèn)題詳解
- 詳解Angular4中路由Router類(lèi)的跳轉(zhuǎn)navigate
- Angular2學(xué)習(xí)筆記——詳解路由器模型(Router)
- 淺析angularJS中的ui-router和ng-grid模塊
- Angularjs中UI Router的使用方法
- angular基于路由控制ui-router實(shí)現(xiàn)系統(tǒng)權(quán)限控制
- AngularJS ui-router (嵌套路由)實(shí)例
- 詳解Angular路由 ng-route和ui-router的區(qū)別
- Angular中使用ui router實(shí)現(xiàn)系統(tǒng)權(quán)限控制及開(kāi)發(fā)遇到問(wèn)題
相關(guān)文章
AngularJS基礎(chǔ) ng-hide 指令用法及示例代碼
本文主要介紹AngularJS ng-hide 指令,這里整理了ng-hide指令的基礎(chǔ)資料,并附實(shí)例代碼,有興趣的小伙伴參考下2016-08-08
div實(shí)現(xiàn)自適應(yīng)高度的textarea實(shí)現(xiàn)angular雙向綁定
本文主要介紹了div實(shí)現(xiàn)自適應(yīng)高度的textarea,實(shí)現(xiàn)angular雙向綁定的方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
深入學(xué)習(xí)JavaScript的AngularJS框架中指令的使用方法
這篇文章主要介紹了深入學(xué)習(xí)JavaScript的AngularJS框架中指令的使用方法,指令的使用是Angular入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-03-03
AngularJS基礎(chǔ) ng-include 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-include 指令,這里對(duì)ng-include的基本知識(shí)做了整理,并附有代碼實(shí)例,有需要的朋友可以參考下2016-08-08
Angularjs中ng-repeat的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Angularjs中ng-repeat的簡(jiǎn)單實(shí)例的相關(guān)資料,這里提供兩個(gè)實(shí)例方法幫助大家徹底掌握這部分內(nèi)容,需要的朋友可以參考下2017-08-08
angularjs定時(shí)任務(wù)的設(shè)置與清除示例
本篇文章主要介紹了angularjs定時(shí)任務(wù)的設(shè)置與清除示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
AngularJS實(shí)現(xiàn)網(wǎng)站換膚實(shí)例
這篇文章主要為大家詳細(xì)介紹了AngularJS實(shí)現(xiàn)網(wǎng)站換膚的簡(jiǎn)單實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
AngularJS 模塊詳解及簡(jiǎn)單實(shí)例
本文主要介紹AngularJS 模塊,這里幫大家整理了相關(guān)資料,詳細(xì)介紹了AngularJS的基礎(chǔ)知識(shí),有需要的朋友可以參考下2016-07-07
整理AngularJS框架使用過(guò)程當(dāng)中的一些性能優(yōu)化要點(diǎn)
這篇文章主要介紹了AngularJS框架使用過(guò)程當(dāng)中的一些性能優(yōu)化要點(diǎn),Angular通常被用來(lái)制作大型單頁(yè)應(yīng)用,因而性能問(wèn)題也是必須考慮的因素,需要的朋友可以參考下2016-03-03

