angular 6 守卫 Observable 挂
angular 6 guard Observable hanging
首先我创建了 2 个守卫,如果没有导航到登录路由,则检查用户是否登录
这是代码
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthenticationService } from '../auth/authentication.service';
import { Router } from '@angular/router';
import { tap, map, take } from 'rxjs/operators';
import {of} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class IsUserGuard implements CanActivate {
constructor( private auth:AuthenticationService, private router:Router){}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.user$.pipe(
take(1),
map(user => !!user),
tap(isAuthenticated => {
if(!isAuthenticated){
console.log('must login');
this.router.navigate(['/login']);
}
})
)
}
}
所以,它工作得很好但是第二个守卫的问题我检查用户登录他不允许再次浏览登录路由所以我创建这个守卫
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
import { AuthenticationService } from '../auth/authentication.service';
import { tap, take, map, } from 'rxjs/operators';
import { of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class IsGuestGuard implements CanActivate {
constructor(private router:Router, private auth:AuthenticationService){}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.user$.pipe(
take(1),
map(user => !!user),
tap(isAuthenticated => {
if(isAuthenticated){
console.log('not a allow')
this.router.navigate(['']);
}
else{
console.log('allowe to show')
return true
//return of(true) give me the same resault
}
}),
)
}
}
它正在工作并且控制台打印 'allowe to show' 但是页面没有出现所以我觉得我在循环中不要停止,如果我 return true 而不是我的代码在第二守卫页面工作
这也是一个路线代码
const routes: Routes = [
{
path: '',
component: PagesComponent,
canActivate:[IsUserGuard],
children: [
{
path: '',
loadChildren: './components/dashboard/dashboard.module#DashboardModule'
},
]
},
{
path: 'login',
canActivate: [IsGuestGuard],
loadChildren: './auth/auth.module#AuthModule',
},
{
path: '404',
component: ErrorPageComponent
},
{
path: 'error/:type',
component: ErrorPageComponent
},
];
在@JeffFairley @fan-cheung 的帮助下,这段代码正在运行,谢谢大家
export class IsGuestGuard implements CanActivate {
constructor(private router:Router, private auth:AuthenticationService){}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.user$.pipe(
map(isAuthenticated => {
if(isAuthenticated){
this.router.navigate(['/']);
return false
}
return true
})
)
}
}
布尔值已经被 map(user=>!!user)
编辑过 return,tap
是一个副作用运算符。尝试将 tap 替换为 map()
和 return 正确的值,看看它是否有效。
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.user$.pipe(
take(1),
map(user => !!user),
map(isAuthenticated => {
if(isAuthenticated){
console.log('not a allow')
this.router.navigate(['']);
return false
}
console.log('allowe to show')
return true
//return of(true) give me the same resault
}),
)
}
首先我创建了 2 个守卫,如果没有导航到登录路由,则检查用户是否登录
这是代码
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthenticationService } from '../auth/authentication.service';
import { Router } from '@angular/router';
import { tap, map, take } from 'rxjs/operators';
import {of} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class IsUserGuard implements CanActivate {
constructor( private auth:AuthenticationService, private router:Router){}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.user$.pipe(
take(1),
map(user => !!user),
tap(isAuthenticated => {
if(!isAuthenticated){
console.log('must login');
this.router.navigate(['/login']);
}
})
)
}
}
所以,它工作得很好但是第二个守卫的问题我检查用户登录他不允许再次浏览登录路由所以我创建这个守卫
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
import { AuthenticationService } from '../auth/authentication.service';
import { tap, take, map, } from 'rxjs/operators';
import { of } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class IsGuestGuard implements CanActivate {
constructor(private router:Router, private auth:AuthenticationService){}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.user$.pipe(
take(1),
map(user => !!user),
tap(isAuthenticated => {
if(isAuthenticated){
console.log('not a allow')
this.router.navigate(['']);
}
else{
console.log('allowe to show')
return true
//return of(true) give me the same resault
}
}),
)
}
}
它正在工作并且控制台打印 'allowe to show' 但是页面没有出现所以我觉得我在循环中不要停止,如果我 return true 而不是我的代码在第二守卫页面工作
这也是一个路线代码
const routes: Routes = [
{
path: '',
component: PagesComponent,
canActivate:[IsUserGuard],
children: [
{
path: '',
loadChildren: './components/dashboard/dashboard.module#DashboardModule'
},
]
},
{
path: 'login',
canActivate: [IsGuestGuard],
loadChildren: './auth/auth.module#AuthModule',
},
{
path: '404',
component: ErrorPageComponent
},
{
path: 'error/:type',
component: ErrorPageComponent
},
];
在@JeffFairley @fan-cheung 的帮助下,这段代码正在运行,谢谢大家
export class IsGuestGuard implements CanActivate {
constructor(private router:Router, private auth:AuthenticationService){}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.user$.pipe(
map(isAuthenticated => {
if(isAuthenticated){
this.router.navigate(['/']);
return false
}
return true
})
)
}
}
布尔值已经被 map(user=>!!user)
编辑过 return,tap
是一个副作用运算符。尝试将 tap 替换为 map()
和 return 正确的值,看看它是否有效。
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.auth.user$.pipe(
take(1),
map(user => !!user),
map(isAuthenticated => {
if(isAuthenticated){
console.log('not a allow')
this.router.navigate(['']);
return false
}
console.log('allowe to show')
return true
//return of(true) give me the same resault
}),
)
}