我使用角2.4版和路由器版"^ 3.4.10".
我正在尝试使用auth guard服务来处理重定向url.
当用户点击url'domain/assignment/3/detail'并且如果用户没有登录,则用户重定向到'domain/login'页面.当用户成功登录系统,然后重定向到用户尝试访问的"domain/assignment/3/detail"前一个URL.
我在分配模块上实现了CanLoad guard.因此,当用户尝试访问url'domain/assignment/3/detail'并且如果用户未登录时,url将存储到authservice(this.authService.redirectUrl)的redirectUrl属性中.
所以问题来自于我的情况.我无法获得用户点击的网址的完整路径.我在CanLoad后卫中获得"任务"而非"任务/ 3 /细节".我怎样才能获得完整路径,以便我可以将用户重定向到CanLoad后卫内的正确路径.
CanLoad:
canLoad(route: Route): boolean {
let url = `/${route.path}`; // here i got url path 'assignment' instead 'assignment/3/detail'
return this.checkLogin(url);
}
Run Code Online (Sandbox Code Playgroud)
主要路由 app.routes.ts
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', component: LoginComponent},
{
path: 'assignment',
loadChildren: './assignment/assignment.module#AssignmentModule',
canLoad: [AuthGuard]
},
{ path: '**', redirectTo: '', pathMatch: 'full' }];
Run Code Online (Sandbox Code Playgroud)
分配路由: assignment-routing.ts
const assignmentRoutes: Routes = [
{
path: '',
component: AssignmentComponent,
canActivate: [AuthGuard]
children: [
{
path: '',
canActivateChild: [AuthGuard],
children: [
{
path: ':assignmentId/detail', component: AssignmentDetailComponent,
canActivate: [AuthGuard]
}
]
}]
}];
Run Code Online (Sandbox Code Playgroud)
AuthGuard:auth-gurad.service.ts
import { Injectable } from '@angular/core';
import {
CanActivate, Router,
ActivatedRouteSnapshot,
RouterStateSnapshot,
CanActivateChild,
NavigationExtras,
CanLoad, Route
} from '@angular/router';
import { AuthService } from './auth.service';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
constructor(private authService: AuthService, private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
let url: string = state.url;
return this.checkLogin(url);
}
canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
return this.canActivate(route, state);
}
canLoad(route: Route): boolean {
let url = `/${route.path}`; // here i got url path 'assignment' instead 'assignment/3/detail'
return this.checkLogin(url);
}
checkLogin(url: string): boolean {
if (this.authService.isLoggedIn) {
if(this.authService.redirectUrl!=null){
let redirectUrl = this.authService.redirectUrl;
this.authService.redirectUrl = null;
this.this.router.navigate([redirectUrl]);
}
return true;
}
// Store the attempted URL for redirecting
this.authService.redirectUrl = url;
// Navigate to the login page
this.router.navigate(['/login']);
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您看一下canLoad方法的签名,则segments可以使用第二个参数来生成url。
canLoad(route: Route, segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean
Run Code Online (Sandbox Code Playgroud)
您可以使用以下代码生成完整路径:
canLoad(route: Route, segments: UrlSegment[]): boolean {
const fullPath = segments.reduce((path, currentSegment) => {
return `${path}/${currentSegment.path}`;
}, '');
console.log(fullPath);
}
Run Code Online (Sandbox Code Playgroud)
注意:使用此方法,您将获得完整路径,但不会获得任何查询参数。
我有这个完全相同的问题。这些问题相关
他们甚至对此都有一个公开的PR,尚未合并
现在的解决方法似乎是用替换该canLoad方法,在canActivate那里您可以访问完整路径,当然不好的是您正在加载模块
编辑:同样为您分配路由,没有必要canActivate为每个子路由调用。在父路由上定义它后,应对所有子路由应用保护措施。
小智 5
github问题上的这个回复的解决方法对我来说非常有效。
export class AuthnGuard implements CanLoad {
constructor(private authnService: AuthnService, private router: Router) { }
canLoad( ): Observable<boolean> | Promise<boolean> | boolean {
const navigation = this.router.getCurrentNavigation();
let url = '/';
if (navigation) {
url = navigation.extractedUrl.toString();
}
this.authnService.storeRedirectUrl(url);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2616 次 |
| 最近记录: |