詳解Angular之路由基礎(chǔ)
一、路由相關(guān)對(duì)象

Router和RouterLink作用一樣,都是導(dǎo)航。Router是在Controller中用的,RouterLink是在模版中用到。
二、路由對(duì)象的位置

1、Routes對(duì)象
配置在模塊中。Routes由一組配置信息組成,每個(gè)配置信息至少包含兩個(gè)屬性,Path和Component。
2、RouterOutlet
在模版中
3、RouterLink
指令,在模版中生成鏈接改變URL
4、Router
在Controller中,調(diào)用Router對(duì)象的navigate方法,路由切換。
5、ActivatedRoute
路由時(shí)候通過URL傳遞數(shù)據(jù),數(shù)據(jù)會(huì)保存在ActivatedRoute對(duì)象中。
三、路由配置
使用ng new --routing參數(shù)時(shí)候會(huì)多生成出來一個(gè)app-routing.module.ts文件
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
會(huì)自動(dòng)imports到app.module.ts中。
生成兩個(gè)組件home組件和component組件。
const routes: Routes = [
{path: '', component : HomeComponent}, //路徑為空
{path: 'product', component: ProductComponent}
];
注意:
1、path路徑配置不能以斜杠開頭,不能配置成path:'/product'。
因?yàn)锳ngular路由器會(huì)解析和生成url,不用/開頭是為了在多個(gè)視圖之間導(dǎo)航時(shí)能自由的使用相對(duì)路徑和絕對(duì)路徑。
2、在模版中寫路徑時(shí),必須用/開頭。
因?yàn)橛眯备芗?表示要導(dǎo)航到根路由(/)還是子路由(./)。
/就是導(dǎo)航到根路由,從配置根路由那一層找。
<a [routerLink]="['/']">主頁</a>
3、在<router-outlet>下面顯示組件內(nèi)容

4、routerLink參數(shù)是一個(gè)數(shù)組而不是字符串
因?yàn)樵诼酚蓵r(shí)候可以傳遞參數(shù)。
四、代碼中通過Router對(duì)象導(dǎo)航
模版上加一個(gè)按鈕
<input type="button" value="商品詳情" (click)="toProductDetails()">
controller中使用router.navigate導(dǎo)航。
navigate參數(shù)和routerLink參數(shù)配置一樣。
import { Component } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private router:Router){
}
toProductDetails(){
this.router.navigate(['/product']);
}
}
點(diǎn)按鈕和點(diǎn)鏈接效果一樣。

五、配置不存在的路徑
生成code404組件顯示頁面不存在。
路由匹配先匹配者優(yōu)先,所以**通配符路由要放最后。
const routes: Routes = [
{ path: '', component: HomeComponent }, //路徑為空
{ path: 'product', component: ProductComponent },
{ path: '**', component: Code404Component }
];

六、重定向路由
一個(gè)地址重定向到另一個(gè)指定組件
www.aaa.com => www.aaa.com/products
www.aaa.com/x => www.aaa.com/y 用戶可能已經(jīng)收藏了x地址。
用重定向路由
const routes: Routes = [
{ path: '', redirectTo : 'home', pathMatch:'full' }, //路徑為空
{ path: 'home', component: HomeComponent },
{ path: 'product', component: ProductComponent },
{ path: '**', component: Code404Component }
];
七、在路由時(shí)候傳遞數(shù)據(jù)
有3種方式
1、在查詢參數(shù)中傳遞數(shù)據(jù)

2、在路由路徑中傳遞數(shù)據(jù)
定義路由路徑時(shí)就要指定參數(shù)名字,在實(shí)際路徑中攜帶參數(shù)。

3、在路由配置中傳遞數(shù)據(jù)

以上就是詳解Angular之路由基礎(chǔ)的詳細(xì)內(nèi)容,更多關(guān)于Angular之路由基礎(chǔ)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在AngularJS中如何使用谷歌地圖把當(dāng)前位置顯示出來
這篇文章給的大家介紹在AngularJS中如何使用谷歌地圖把當(dāng)前位置顯示出來,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,對(duì)angularjs 谷歌地圖相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
AngularJS 基礎(chǔ)ng-class-even指令用法
本文主要介紹AngularJS ng-class-even 指令,這里整理了ng-class-even基礎(chǔ)知識(shí)資料,并附實(shí)例代碼和效果圖,學(xué)習(xí)AngularJS指令的朋友可以看下2016-08-08
Angular 數(shù)據(jù)請求的實(shí)現(xiàn)方法
本篇文章主要介紹了Angular 數(shù)據(jù)請求的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
詳解Angular路由動(dòng)畫及高階動(dòng)畫函數(shù)
本文主要講解了Angular的路由動(dòng)畫和高階動(dòng)畫函數(shù),對(duì)此感興趣的同學(xué),可以把代碼親自實(shí)驗(yàn)一下,理解其原理。2021-05-05
Angular.JS中的指令引用template與指令當(dāng)做屬性詳解
這篇文章主要介紹了Angular.JS中的指令引用template與指令當(dāng)做屬性的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
angular動(dòng)態(tài)刪除ng-repaeat添加的dom節(jié)點(diǎn)的方法
本篇文章主要介紹了angular動(dòng)態(tài)刪除ng-repaeat添加的dom節(jié)點(diǎn)的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-07-07
Angular.js 實(shí)現(xiàn)數(shù)字轉(zhuǎn)換漢字實(shí)例代碼
這篇文章主要介紹了Angular.js 實(shí)現(xiàn)數(shù)字轉(zhuǎn)換漢字實(shí)例代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Angular.js實(shí)現(xiàn)多個(gè)checkbox只能選擇一個(gè)的方法示例
這篇文章主要給大家介紹了利用Angular.js實(shí)現(xiàn)多個(gè)checkbox只能選擇一個(gè)的方法,文中給出了詳細(xì)的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,下面來一起看看吧。2017-02-02

