[OK]を、この点を説明する必要があり、私の取り除か例です。
@Injectable()
export class AuthGuardService implements CanActivate
{
toUrl;
constructor(public authenticationService: AuthenticationService,
public router: Router)
{
}
canActivate(route, state): boolean
{
this.toUrl = state.url; //This is the url where its going
if (this.authenticationService.isLoggedIn()) return true;
this.router.navigate(['/login', {redirect: this.toUrl}]);
}
}
し、ログインコンポーネントに任意のulrsをリダイレクトをチェックするngOnInitを使用します。
export class LoginComponent
{
redirect;
constructor(
private authenticationService: AuthenticationService,
private route: ActivatedRoute,
private router: Router)
{
}
ngOnInit()
{
this.redirect = this.route.snapshot.params['redirect'];
}
logIn(): void
{
this.authenticationService
.login(this.searchParams)
.subscribe(
() =>
{
this.logInSuccess();
},
error =>
{
this.logInFail(error)
})
}
logInSuccess(): void
{
if (this.redirect)
{
this.router.navigateByUrl(this.redirect);
}
else
{
this.router.navigate([''])
}
}
}
だから、道ルートガードの動作は、ガードのcanActivateメソッドにヒットして、ユーザーがすでに認証されているかどうかを判断します。その方法では、this.router.urlを取得しようとしましたが、新しいルートではなく、保護されたルートリンクをクリックしたときのユーザーのルートと同じです。アドレスバーのURLは現在のルートであり、新しい保護されたルートではありません。だから私は、ユーザーがクリックした保護ルートを知る方法がありません。あるいは、少なくとも私はそうは思わない、誰かに私にその新しいルートを取得する方法を教えて、私があなたが提案したものを正確に行うことができると願っている。 – Eddie
私のアプリでこれを実装することは、これをしばらく行うことを意味しています。私はそれをどのように共有します。 –
私はあなたが何を意味するかを見て、私はそれが行く新しいルートを取得しようとしています。どこにも見つけられないようだ。 コンストラクタ(プライベートスナップショット:ActivatedRouteSnapshot){ せnewRoute = this.snapshot._futureSnapshot._routerState.url –