我正在尝试将用户重定向到登录页面,如果用户没有登录。一旦用户登录,我想让用户回到上一页。但我收到的错误为StaticInjectorError[RouterStateSnapshot]: NullInjectorError: No provider for RouterStateSnapshot!
。
以下是CanLoad功能的代码:
@Injectable()
export class TokenGuardService implements CanLoad {
constructor(private authService: AuthService, private router: Router, private snapshot: RouterStateSnapshot) {}
canLoad(route: Route): Observable<boolean> | Promise<boolean> | boolean {
if(this.authService.doesTokenExist()) {
return true;
} else {
this.router.navigate(['/login'], { queryParams: { returnUrl: this.snapshot.url }});
return false;
}
}
这是代码我的组件
export class LoginComponent implements OnInit {
constructor(private loginService: LoginService, private router: Router, private route: ActivatedRoute) { }
ngOnInit() {
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/dashboard';
}
onLoginFormSubmit() {
//Some success scenario
if(success) {
this.router.navigate([this.returnUrl]);
}
}
请帮我解决这个问题。