詳解angular筆記路由之a(chǎn)ngular-router
本文介紹了angular筆記路由之a(chǎn)ngular-router,分享給大家,具體如下:
創(chuàng)建項目
ng new router --routing \\ 加routing參數(shù) \\ 會新增一個app-routing.module.ts 文件
路由的基本使用
名稱 | 簡介 |
---|---|
Routes | 路由的配置,保存著哪一個URL對應(yīng)展示哪一個組件和在哪一個RouterOutler展示 |
RouterOutler | 在HTML中標(biāo)記路由內(nèi)容呈現(xiàn)的占位符指令 |
Router | 運行時執(zhí)行的路由對象,可以通過navigate()和navigateByUrl()方法來導(dǎo)航到指定路由 |
RouterLink | 在HTML中聲明導(dǎo)航的指令 |
ActivatedRoute | 當(dāng)前激活的路由對象,保存著當(dāng)前路由信息,路由地址,路由參數(shù) |
路由對象圖示
路由基本配置
const routes:Routes = [ {path:'',component:HomeComponent}, \\ 注意path里面不要加\線 {path:'app',component:AdminComponent} ];
路由通配符配置
{path:'**',component:Code404Component} // 配置里面加一條代表沒有的都往這里,注意這里不能寫在前面,不然angualr的路由會優(yōu)先匹配這里
HTML里面跳轉(zhuǎn)鏈接
<a [routerLink]="['/']">主頁</a> <a [routerLink]="['/app']">后臺</a> <router-outlet></router-outlet>
在js里面跳轉(zhuǎn)路由
<input type='button' value='跳轉(zhuǎn)到后臺' (click)="toApp()">
constructor(private router:Router){ } // 點擊事件 toApp(){ this.router.navigate(['/app']) }
路由數(shù)據(jù)傳遞
1、在查詢參數(shù)中傳遞數(shù)據(jù)
/app?id=2 => ActivatedRoute.queryParams[id] //html寫法 <a [routerLink]="['/app']" [queryParams]="{id:1}">后臺</a> // js寫法 private appId:number constructor(private routerInfo:ActivatedRoute) { } ngOnInit() { this.appId = this.routerInfo.snapshot.queryParams['id'] }
2、在路由路徑中傳遞數(shù)據(jù)
{path:/app/:id} => /app/1 => ActivatedRoute.params[id] // 必須先定義好 <a [routerLink]="['/app',1]">后臺</a>
3、在路由配置中傳遞數(shù)據(jù)
{path:/product,component:Appcomponent,data:[IsProd:true]} => ActivatedRoute.data[0][IsProd]
參數(shù)快照和參數(shù)訂閱
snapshot 是參數(shù)快照當(dāng)路由進入該組件的時候,然后再點擊按鈕進入該路由路由里面的的ngOnInit()方法只執(zhí)行一次,已經(jīng)被激活,說以第二次這個不會被執(zhí)行
ngOnInit() { this.appId = this.routerInfo.snapshot.queryParams['id'] }
subscribe 是參數(shù)訂閱,這個屬于RxJs的東西
private appId:number constructor(private routerInfo:ActivatedRoute) { } ngOnInit() { this.routerInfo.params.subscribe((params:Params) => { this.appId = params['id'] }) }
重定向路由
{path:'',redirectTo:'/home',pathMatch:'full'},
子路由
{path:'home',component:HomeComponent,children:[ {path:'',component:IndexComponent} ]}, // 記得去HomeComponent,里面添加<router-outlet></router-outlet>指令
輔助路由
// html 視圖部分 <router-outlet></router-outlet> <router-outlet name='aux'></router-outlet> //路由配置部分 {path:'app',Appcomponet,outlet:'aux'}
路由守衛(wèi)
只有當(dāng)用戶已經(jīng)登錄并擁有一些權(quán)限時才能進入
多表單組成的向?qū)?,如注冊流程,只有滿足條件才能進入下一個路由
用戶執(zhí)行操作沒有保存,試圖離開時候進行相關(guān)提示
名稱 | 說明 |
---|---|
CanAxtivate | 處理導(dǎo)航到某路由 |
CanDeactivate | 處理當(dāng)前路由離開 |
Resolve | 在路由激活前獲取路由數(shù)據(jù) |
1.CanAxtivate的使用
// 新建一個文件 import {CanActivate} from '@angular/router' export class LoginGuard implements CanActivate{ // 路由會根據(jù)這個方法的返回如果返回false就拒絕訪問 canActivate(){ let isLOgin:boolean = true; return isLOgin; } }
canActivate是一個數(shù)組,可以接收多個,當(dāng)每一個都返回true時候才允許
// 修改路由配置,添加一個屬性canActivate {path:'home',component:HomeComponent,children:[ {path:'',component:IndexComponent} ],canActivate:[LoginGuard]}, 修改NgModule providers:[LoginGuard]
2.CanDeactivate的使用
// 新建一個文件 import {CanDeactivate} from '@angular/router' import {AppComponent} from "../app.component"; /** * 處理用戶離開 * 接收一個泛型 */ export class OutGuard implements CanDeactivate<AppComponent>{ // component 里面保存著AppComponent的數(shù)據(jù) canDeactivate(component:AppComponent){ return window.confirm('您還沒有保存確定要離開嗎?') } }
修改路由的配置
{path:'home',component:HomeComponent,children:[ {path:'',component:IndexComponent} ],canActivate:[LoginGuard],canDeactivate:[OutGuard]},
providers:[LoginGuard,OutGuard]
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJs入門教程之環(huán)境搭建+創(chuàng)建應(yīng)用示例
這篇文章主要介紹了AngularJs入門教程之環(huán)境搭建+創(chuàng)建應(yīng)用的方法,較為詳細的分析了AngularJS的功能、下載及應(yīng)用創(chuàng)建方法,需要的朋友可以參考下2016-11-11ng-events類似ionic中Events的angular全局事件
這篇文章主要介紹了ng-events類似ionic中Events的angular全局事件,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09在AngularJS中使用jQuery的zTree插件的方法
這篇文章主要介紹了在AngularJS中使用jQuery的zTree插件的方法,Angular中集成了jqLite,但還不是完全版的jQuery,需要的朋友可以參考下2016-04-04深入淺析AngularJS中的一次性數(shù)據(jù)綁定 (bindonce)
這篇文章主要介紹了AngularJS中的一次性數(shù)據(jù)綁定 (bindonce)知識,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05