如何获取用户尝试在角度 2 的 canActivate 防护中打开的路线?
我怎样才能让守卫监视每个路由参数的变化,例如。路线内的ID更改?
import { Injectable } from '@angular/core';
import {Observable, Observer, Subject} from "rxjs/Rx";
@Injectable()
export class RightsGuard implements CanActivate {
constructor(
private router: Router,
private route: ActivatedRoute
// private state: RouterStateSnapshot
) { }
canActivate(): Observable<boolean> | boolean {
var targetUrl = ???
// var targetUrl = this.router.url // is showing previous url?
return true;
}
}
Angular 2中的建议答案get current route in guard usingprivate state: RouterStateSnapshot
然后this.route.url
对我不起作用,因为它会引发 DI 错误:
Error: Uncaught (in promise): Error: DI Error
Error: DI Error