简单的Angular 2问题:当我将LoginStateSnapshot注入我的Login Component中的构造函数时,为什么会出现DI(依赖注入)错误?我正在尝试在注销之前获取URL,因此我可以在用户重新登录时传递它(因此它将加载其上次访问的组件/页面) . 这比我预期的要困难得多 . 即使在我的构造函数中导入并包含RouterStateSnapshot之后,我也会收到DI错误 . 这是我在登录组件中设置它的方式:
import { ActivatedRoute, ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
constructor(private router: Router,
private route: ActivatedRoute,
private authenticationService: AuthenticationService,
private alertService: AlertService,
private state: RouterStateSnapshot,
private authGuardService: AuthGuardService,
private idle: Idle)
{
这有什么问题?我错过了与ActivatedRoute,ActivatedRouteSnapshot等不同的RouterStateSnapshot - 我可以导入并注入没有问题?为什么我不能以相同的方式注入RouterStateSnapshot?
顺便说一句,我可以在我的authGuardService中使用RouterStateSnapshot而不会出现问题 . 它在我的canActivate()函数中使用 - 并返回正确的结果 . 那么这种情况有什么不同呢?为什么我不能在我的登录组件中使用它?
1 回答
我想你可以从
RouterState
获得RouterStateSnapshot
. 考虑这个例子: