我有这种情况:
- 用户单击链接,例如来自 Skype 之类的链接。
- 用户想要访问的 url 要求用户登录。
- 登录后,用户应该被重定向到他点击的那个链接,而不是应用程序的根目录。
到目前为止,这是我的登录组件:
login() {
this.loading = true;
this.authenticationService.login(this.model.username, this.model.password)
.takeWhile(() => this.isAlive)
.subscribe(result => {
if (result) {
this.router.navigate(['/']);
} else {
this.error = 'Error.';
this.loading = false;
}
},
err => {
this.translation[err.description] ? this.error = this.translation[err.description] : this.error = this.error = err.description;
this.loading = false;
})
}
我不知道该怎么做,但我认为我应该看看 routerHistory 的真实陈述if(result)。
如何正确地做到这一点?
编辑和解决方案
我终于解决了这个问题。
CanActivate
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot) {
console.log(state.url);
}
state.url包含以前的 url,所以我只是将它放入主题并在我需要的地方订阅。