Angular中AuthGuard路由守衛(wèi)的創(chuàng)建使用
Angular中的AuthGuard
Angular 中的 AuthGuard 是一個路由守衛(wèi),它用于保護(hù)某些路由,只有當(dāng)用戶經(jīng)過身份驗證并具有訪問權(quán)限時,才允許他們訪問。AuthGuard 通常與路由配置一起使用,以確保用戶無法直接訪問需要身份驗證的頁面。
創(chuàng)建
AuthGuard 是一個 Angular 服務(wù),可以使用以下命令來創(chuàng)建它:
ng g guard auth
上面的命令將生成一個名為“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 是通過實現(xiàn) CanActivate 接口來定義的。CanActivate 接口包含一個名為 canActivate()
的方法,該方法決定了當(dāng)用戶嘗試訪問受保護(hù)的路由時應(yīng)該執(zhí)行哪些操作。
在 AuthGuard 中,我們注入了一個名為 AuthService
的服務(wù)和 Router
對象。AuthService
用于檢查用戶是否已經(jīng)登錄,而 Router
用于導(dǎo)航到登錄頁面或者用戶試圖訪問的頁面。在 canActivate()
方法中,我們首先調(diào)用 this.authService.isLoggedIn()
方法來檢查用戶是否已經(jīng)登錄。如果用戶已經(jīng)登錄,我們返回 true,表示用戶可以訪問該路由。否則,我們調(diào)用 this.router.navigate(['/login'])
方法,將用戶重定向到登錄頁面,然后返回 false,表示用戶無法訪問該路由。
AuthGuard保護(hù)一個路由
要使用 AuthGuard 來保護(hù)一個路由,我們需要在路由配置中將 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)登錄時才能訪問 /dashboard
路由。
以上就是Angular中AuthGuard路由守衛(wèi)的創(chuàng)建使用的詳細(xì)內(nèi)容,更多關(guān)于Angular AuthGuard創(chuàng)建使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
AngularJS入門心得之directive和controller通信過程
Angular JS (Angular.JS) 是一組用來開發(fā)Web頁面的框架、模板以及數(shù)據(jù)綁定和豐富UI組件,接下來通過本文給大家介紹AngularJS入門心得之directive和controller通信過程,對angularjs相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-01-01AngularJs篇:使用AngularJs打造一個簡易權(quán)限系統(tǒng)的實現(xiàn)代碼
本篇文章主要介紹了AngularJs篇:使用AngularJs打造一個簡易權(quán)限系統(tǒng)的實現(xiàn)代碼,具有一定的參考價值,有興趣的可以了解一下。2016-12-12使用Angular CDK實現(xiàn)一個Service彈出Toast組件功能
本文主要寫用cdk實現(xiàn)一個簡單的Toast組件,使用的是cdk中的overlay模塊,需要手動安裝環(huán)境,具體安裝方法及相關(guān)實現(xiàn)代碼跟隨小編一起看看吧2021-07-07Angular2中監(jiān)聽數(shù)據(jù)更新的方法
今天小編就為大家分享一篇Angular2中監(jiān)聽數(shù)據(jù)更新的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Angular5升級RxJS到5.5.3報錯:EmptyError: no elements in sequence的解
這篇文章主要給大家介紹了關(guān)于Angular5升級RxJS到5.5.3報錯:EmptyError: no elements in sequence的解決方法,文中介紹了兩個解決方法,大家可以選擇使用,需要的朋友可以參考借鑒,下面來一起看看吧。2018-04-04在Angular中實現(xiàn)一個級聯(lián)效果的下拉框的示例代碼
這篇文章主要介紹了在Angular中實現(xiàn)一個級聯(lián)效果的下拉框的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05AngularJs學(xué)習(xí)第五篇從Controller控制器談?wù)?scope作用域
這篇文章主要介紹了AngularJs(五)從Controller控制器談?wù)?scope作用域 的相關(guān)資料,需要的朋友可以參考下2016-06-06