gpt4 book ai didi

angular - 类型错误 : Cannot read property 'canDeactivate' of null

转载 作者:行者123 更新时间:2023-12-03 18:32:28 24 4
gpt4 key购买 nike

我正在尝试使用 canDeactivate 路由器防护。我传入的组件 (ClaimsViewComponent) 在代码第一次运行时为空。但在随后的运行中,代码按预期运行。
我们试图弄清楚为什么组件在第一次运行时为空。

这是 canDeactivate 保护代码:

@Injectable()
export class ConfirmDeactivateGuard implements
CanDeactivate<ClaimsViewComponent> {
canDeactivate(target: ClaimsViewComponent): boolean {

if (target.canDeactivate()) {
return window.confirm('Do you really want to cancel?');
}

return true;
}
}

这是路由模块代码:
const routes: Routes = [
{ path: ':type', component: ClaimsViewComponent, canDeactivate:
[ConfirmDeactivateGuard] }
];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})

这是完整的错误堆栈:
TypeError: Cannot read property 'canDeactivate' of null
at ConfirmDeactivateGuard.canDeactivate (confirm-deactivate-guard.ts:16)
at MergeMapSubscriber.eval [as project] (router.js:3933)
at MergeMapSubscriber._tryNext (mergeMap.js:128)
at MergeMapSubscriber._next (mergeMap.js:118)
at MergeMapSubscriber.Subscriber.next (Subscriber.js:95)
at ArrayObservable._subscribe (ArrayObservable.js:116)
at ArrayObservable.Observable._trySubscribe (Observable.js:172)
at ArrayObservable.Observable.subscribe (Observable.js:160)
at MergeMapOperator.call (mergeMap.js:92)
at Observable.subscribe (Observable.js:157)
at ConfirmDeactivateGuard.canDeactivate (confirm-deactivate-guard.ts:16)
at MergeMapSubscriber.eval [as project] (router.js:3933)
at MergeMapSubscriber._tryNext (mergeMap.js:128)
at MergeMapSubscriber._next (mergeMap.js:118)
at MergeMapSubscriber.Subscriber.next (Subscriber.js:95)
at ArrayObservable._subscribe (ArrayObservable.js:116)
at ArrayObservable.Observable._trySubscribe (Observable.js:172)
at ArrayObservable.Observable.subscribe (Observable.js:160)
at MergeMapOperator.call (mergeMap.js:92)
at Observable.subscribe (Observable.js:157)
at resolvePromise (zone.js:814)
at resolvePromise (zone.js:771)
at eval (zone.js:873)
at ZoneDelegate.invokeTask (zone.js:421)
at Object.onInvokeTask (core.js:4740)
at ZoneDelegate.invokeTask (zone.js:420)
at Zone.runTask (zone.js:188)
at drainMicroTaskQueue (zone.js:595)

谢谢你。

最佳答案

我通过在 page.module.ts 而不是 app.module.ts 中添加 canDeactivate 解决了这个问题

关于angular - 类型错误 : Cannot read property 'canDeactivate' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51450323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com