我目前正在尝试为我的角度应用程序实现CanDeactivate Guard,该应用程序已经具有HTTP拦截器来解析身份验证。 但是添加CanDeactivate防护会引发错误,如下所述。
我已经在AppModule的providers数组中注册了实现CanDeactivate接口的Class。 我已经在路由的canDeactivate属性中注册了实现CanDeactivate接口的类。
实现CanDeactivate接口的类:
export interface CanComponentDeactivate {
canComponentDeactivate(): boolean;
}
@Injectable({providedIn: 'root'})
export class AppRegisterDeactivateGuard implements CanDeactivate<AppRegisterComponent> {
canDeactivate(
component: AppRegisterComponent,
currentRoute: ActivatedRouteSnapshot,
currentState: RouterStateSnapshot
): Observable<boolean>|Promise<boolean>|boolean {
return component.isDeactivated;
}
}
其中应用了停用保护的组件:
@Component({
selector: 'app-app-register',
templateUrl: './app-register.component.html',
styleUrls: ['./app-register.component.css']
})
export class AppRegisterComponent implements OnInit, CanComponentDeactivate {
isDeactivated = false;
@ViewChild('registrationForm') registForm: NgForm;
.
.
canComponentDeactivate() {
if (this.registForm.dirty) {
this.isDeactivated = confirm('You have modified changes in the form. \nDo you still want to navigate away ? ');
}
return this.isDeactivated;
}
}
该端点的路由定义:
...
{ path: 'register', component: AppRegisterComponent, canDeactivate: ['AppRegisterDeactivateGuard'] },
模块中的提供者数组:
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: AppAuthInterceptService,
multi: true
}, AppRegisterDeactivateGuard],
bootstrap: [AppComponent]
错误
StaticInjectorError(Platform: core)[AppRegisterDeactivateGuard]:
NullInjectorError: No provider for AppRegisterDeactivateGuard!
Error: StaticInjectorError(AppModule)[AppRegisterDeactivateGuard]:
StaticInjectorError(Platform: core)[AppRegisterDeactivateGuard]:
NullInjectorError: No provider for AppRegisterDeactivateGuard!
at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:8896)
at resolveToken (core.js:9141)
at tryResolveToken (core.js:9085)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:8982)
at resolveToken (core.js:9141)
at tryResolveToken (core.js:9085)
at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:8982)
at resolveNgModuleDep (core.js:21218)
at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:21907)
at getToken (router.js:2865)
at resolvePromise (zone.js:831)
at resolvePromise (zone.js:788)
at zone.js:892
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:17290)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
at drainMicroTaskQueue (zone.js:601)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:502)
at invokeTask (zone.js:1744)
答案 0 :(得分:0)
TLDR; 将路线更改为此:
{ path: 'register', component: AppRegisterComponent, canDeactivate: [AppRegisterDeactivateGuard] }
将您的提供商更改为此:
[{
provide: HTTP_INTERCEPTORS,
useClass: AppAuthInterceptService,
multi: true
}]
更长的答案:
同时具有{providedIn: 'root'}
并将其声明为模块中的提供者的AppRegisterDeactivateGuard是不必要的。 See the Angular provider docs。
这里的问题是路由和提供者的结合。
现在您的路线正在通过字符串名称查找令牌,但是可以通过类名称使用。
使用@Injectable({providedIn: 'root'})
的方式将类名用作令牌,它将作为AppRegisterDeactivateGuard提供。
在提供商中拥有AppRegisterDeactivateGuard
的方式 还将使其作为AppRegisterDeactivateGuard可用。
我不建议这样做,但是如果您希望或需要通过字符串名称引用它,则可以将其放在提供程序中,如下所示:
{ provide: 'AppRegisterDeactivateGuard', useClass: AppRegisterDeactivateGuard }
答案 1 :(得分:-1)
您需要以泛型形式传递接口:
export class AppRegisterDeactivateGuard implements CanDeactivate<CanComponentDeactivate > {
然后实现canDeactivate方法