gpt4 book ai didi

Angular 5 导航在守卫返回 Observable 后被取消

转载 作者:行者123 更新时间:2023-12-02 20:25:37 26 4
gpt4 key购买 nike

我有三条路线,其中一条有守卫保护

app.routing-module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { LoginComponent } from './modules/auth/login/login.component';
import { DashboardComponent } from './modules/dashboard/dashboard.component';
import { CustomersComponent } from './modules/customers/customers.component';

import { AuthGuard } from './shared/guards/auth.guard';


const routes: Routes = [
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
{ path: 'home', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: 'customers', component: CustomersComponent },
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [AuthGuard]
})
export class AppRoutingModule { }

守卫获取用户是否通过 ngrx 存储进行身份验证,并返回一个 Observable 以允许或不允许激活路由

import { Injectable } from '@angular/core';
import {
CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
Router
} from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { catchError, map, switchMap, filter, take } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
import { Store } from '@ngrx/store';

import { getUserAuth } from '../../store/reducers';


@Injectable()
export class AuthGuard implements CanActivate {
constructor(private store: Store<any>, private router: Router) { }

getUserAuth(): Observable<any> {
return this.store.select(getUserAuth).pipe(
filter((data: any) => data),
take(1)
);
}

canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
return this.getUserAuth().pipe(
switchMap(() => of(true)),
catchError(() => of(false))
);
}
}

在两条“公共(public)”路线之间切换时,导航工作正常,但一旦尝试访问 protected 路线并被取消(用户未经身份验证),导航就会被取消,并且任何路线都无法访问。另一方面,如果用户已登录,则导航工作正常并且可以访问三个路线。

如果我在 canActivate 方法中返回一个简单的 Observable.of(false),则无法访问 protected 路由,但导航到其他路由仍然有效

canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean> | Promise<boolean> | boolean {
return Observable.of(false);
}

为什么取消导航?

编辑

我忘了说我正在使用 @ngrx/router-store 所以我的 app.module 看起来像:

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
AppRoutingModule,
CoreModule,
StoreModule.forRoot(rootReducers, {metaReducers}),
StoreRouterConnectingModule.forRoot({
stateKey: 'router' // name of reducer key
}),
StoreDevtoolsModule.instrument({ maxAge: 25 }),
EffectsModule.forRoot(rootEffects),
AuthModule,
DashboardModule,
CustomersModule
],
providers: [{ provide: RouterStateSerializer, useClass: CustomSerializer }],
bootstrap: [AppComponent]
})

最佳答案

问题出在这里:

@Injectable()
export class AuthGuard implements CanActivate {
constructor(private store: Store<any>, private router: Router) { }

getUserAuth(): Observable<any> {
return this.store.select(getUserAuth).pipe(
filter((data: any) => data),
^^^^^^
take(1)
);
}

AuthGuard 将暂停导航,直到 Observable 解析为 truefalse。由于您要过滤任何 falsy null 值,因此当用户未经身份验证时,Observable 将不会发出任何内容。您应该将值映射到 true/false:

return this.store.select(getUserAuth).pipe(
map((data: any) => !!data),
^^^^^^^^^^^^^^^^^^^^^^^^^
take(1)
);

关于Angular 5 导航在守卫返回 Observable 后被取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50059208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com