Angular中AuthGuard路由守衛(wèi)的創(chuàng)建使用
Angular中的AuthGuard
Angular 中的 AuthGuard 是一個(gè)路由守衛(wèi),它用于保護(hù)某些路由,只有當(dāng)用戶經(jīng)過(guò)身份驗(yàn)證并具有訪問(wèn)權(quán)限時(shí),才允許他們?cè)L問(wèn)。AuthGuard 通常與路由配置一起使用,以確保用戶無(wú)法直接訪問(wèn)需要身份驗(yàn)證的頁(yè)面。
創(chuàng)建
AuthGuard 是一個(gè) Angular 服務(wù),可以使用以下命令來(lái)創(chuàng)建它:
ng g guard auth
上面的命令將生成一個(gè)名為“auth”的 AuthGuard,并將其添加到 src/app/auth.guard.ts
文件中。
auth.guard.ts
文件中的代碼如下所示:
import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router'; import { Observable } from 'rxjs'; import { AuthService } from './auth.service'; @Injectable({ providedIn: 'root' }) export class AuthGuard implements CanActivate { constructor(private authService: AuthService, private router: Router) {} canActivate( next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { if (this.authService.isLoggedIn()) { return true; } else { this.router.navigate(['/login']); return false; } } }
在上面的代碼中,AuthGuard 是通過(guò)實(shí)現(xiàn) CanActivate 接口來(lái)定義的。CanActivate 接口包含一個(gè)名為 canActivate()
的方法,該方法決定了當(dāng)用戶嘗試訪問(wèn)受保護(hù)的路由時(shí)應(yīng)該執(zhí)行哪些操作。
在 AuthGuard 中,我們注入了一個(gè)名為 AuthService
的服務(wù)和 Router
對(duì)象。AuthService
用于檢查用戶是否已經(jīng)登錄,而 Router
用于導(dǎo)航到登錄頁(yè)面或者用戶試圖訪問(wèn)的頁(yè)面。在 canActivate()
方法中,我們首先調(diào)用 this.authService.isLoggedIn()
方法來(lái)檢查用戶是否已經(jīng)登錄。如果用戶已經(jīng)登錄,我們返回 true,表示用戶可以訪問(wèn)該路由。否則,我們調(diào)用 this.router.navigate(['/login'])
方法,將用戶重定向到登錄頁(yè)面,然后返回 false,表示用戶無(wú)法訪問(wèn)該路由。
AuthGuard保護(hù)一個(gè)路由
要使用 AuthGuard 來(lái)保護(hù)一個(gè)路由,我們需要在路由配置中將 AuthGuard 添加到 canActivate
屬性中。例如:
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { DashboardComponent } from './dashboard/dashboard.component'; import { LoginComponent } from './login/login.component'; import { AuthGuard } from './auth.guard'; const routes: Routes = [ { path: '', component: HomeComponent }, { path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] }, { path: 'login', component: LoginComponent }, ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
在上面的代碼中,我們將 AuthGuard 添加到了 /dashboard
路由的 canActivate
屬性中,這意味著只有當(dāng)用戶已經(jīng)登錄時(shí)才能訪問(wèn) /dashboard
路由。
以上就是Angular中AuthGuard路由守衛(wèi)的創(chuàng)建使用的詳細(xì)內(nèi)容,更多關(guān)于Angular AuthGuard創(chuàng)建使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
AngularJS入門心得之directive和controller通信過(guò)程
Angular JS (Angular.JS) 是一組用來(lái)開發(fā)Web頁(yè)面的框架、模板以及數(shù)據(jù)綁定和豐富UI組件,接下來(lái)通過(guò)本文給大家介紹AngularJS入門心得之directive和controller通信過(guò)程,對(duì)angularjs相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01angularjs實(shí)現(xiàn)的購(gòu)物金額計(jì)算工具示例
這篇文章主要介紹了angularjs實(shí)現(xiàn)的購(gòu)物金額計(jì)算工具,涉及AngularJS事件監(jiān)聽(tīng)、數(shù)值計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2018-05-05AngularJs篇:使用AngularJs打造一個(gè)簡(jiǎn)易權(quán)限系統(tǒng)的實(shí)現(xiàn)代碼
本篇文章主要介紹了AngularJs篇:使用AngularJs打造一個(gè)簡(jiǎn)易權(quán)限系統(tǒng)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12使用Angular CDK實(shí)現(xiàn)一個(gè)Service彈出Toast組件功能
本文主要寫用cdk實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Toast組件,使用的是cdk中的overlay模塊,需要手動(dòng)安裝環(huán)境,具體安裝方法及相關(guān)實(shí)現(xiàn)代碼跟隨小編一起看看吧2021-07-07Angular2中監(jiān)聽(tīng)數(shù)據(jù)更新的方法
今天小編就為大家分享一篇Angular2中監(jiān)聽(tīng)數(shù)據(jù)更新的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08angularjs2 ng2 密碼隱藏顯示的實(shí)例代碼
本篇文章主要介紹了angularjs2 ng2 密碼隱藏顯示的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Angular5升級(jí)RxJS到5.5.3報(bào)錯(cuò):EmptyError: no elements in sequence的解
這篇文章主要給大家介紹了關(guān)于Angular5升級(jí)RxJS到5.5.3報(bào)錯(cuò):EmptyError: no elements in sequence的解決方法,文中介紹了兩個(gè)解決方法,大家可以選擇使用,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2018-04-04在Angular中實(shí)現(xiàn)一個(gè)級(jí)聯(lián)效果的下拉框的示例代碼
這篇文章主要介紹了在Angular中實(shí)現(xiàn)一個(gè)級(jí)聯(lián)效果的下拉框的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05AngularJs學(xué)習(xí)第五篇從Controller控制器談?wù)?scope作用域
這篇文章主要介紹了AngularJs(五)從Controller控制器談?wù)?scope作用域 的相關(guān)資料,需要的朋友可以參考下2016-06-06