我使用角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: …Run Code Online (Sandbox Code Playgroud) comment.component.ts:
import { Component, OnInit } from '@angular/core';
import { Router} from '@angular/router'
import { Comment } from 'comment entity path'
import {CommentService} from 'comment service path'
import { Observable } from 'rxjs/Observable';
@Component({
template: ` <ul><li *ngFor="let comment of comments|async"> {{comment.Name}}</li></ul>`
})
export class CommentComponent implements OnInit {
comments: Observable<comment[]>;
constructor(private router: Router, private commentService: CommentService) {
}
ngOnInit() {
this.comments = this.getComments();
}
getComments() {
return this.commentService.getComments();
}
}
Run Code Online (Sandbox Code Playgroud)
comment.service.ts
import { Injectable } from '@angular/core';
import { …Run Code Online (Sandbox Code Playgroud)