TypeError: Cannot read property 'canDeactivate' of null in IONIC 5
TypeError: Cannot read property 'canDeactivate' of null in IONIC 5
我无法在 Ionic 5 中实现 canDeactivate 保护。以下是我的代码。
model.ts
export interface isDeactivatable {
canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}
离开-page.guard.ts
export class LeavePageGuard implements CanDeactivate<isDeactivatable>{
canDeactivate(
component: isDeactivatable
): Observable<boolean> | Promise<boolean> | boolean {
console.log('LeavePageGuard');
return component.canDeactivate();
}
}
测试-page.ts
export class TestPage implements OnInit, isDeactivatable{
canDeactivate(): Observable<boolean> | Promise<boolean> | boolean {
console.log('canDeactivate in TestPage');
return true;
}
}
首页-routing.Module.ts
const routes: Routes = [
{
path: '',
component: HomePage,
children:[
{
path: 'test',
loadChildren: () => import('../pages/test/test.module').then( m => m.TestPageModule),
canDeactivate: [LeavePageGuard]
}
]
}
]
离开 TestPage
时出现以下错误。
我尝试在 App 模块和 TestPage 模块中添加 LeavePageGuard
作为提供者,但仍然出现相同的错误。
core.js:6014 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'canDeactivate' of null
TypeError: Cannot read property 'canDeactivate' of null
at LeavePageGuard.canDeactivate (leave-page.guard.ts:15)
at router.js:5543
at Array.map (<anonymous>)
at runCanDeactivate (router.js:5532)
at MergeMapSubscriber.project (router.js:5349)
at MergeMapSubscriber._tryNext (mergeMap.js:46)
at MergeMapSubscriber._next (mergeMap.js:36)
at MergeMapSubscriber.next (Subscriber.js:49)
at Observable._subscribe (subscribeToArray.js:3)
at Observable._trySubscribe (Observable.js:42)
at resolvePromise (zone-evergreen.js:797)
at resolvePromise (zone-evergreen.js:754)
at zone-evergreen.js:858
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:39680)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559)
at invokeTask (zone-evergreen.js:469)
at ZoneTask.invoke (zone-evergreen.js:454)
解决方案非常简单,我找到了 。问题在于延迟加载。大多数解决方案都不是针对延迟加载问题。
我将 canDeativate 守卫从应用程序路由移动到该特定页面的模块路由。工作得很好。
测试-rouring.module.ts
const routes: Routes = [
{
path: '',
component: TestPage
canDeactivate: [ LeavePageGuard ]
}
我无法在 Ionic 5 中实现 canDeactivate 保护。以下是我的代码。
model.ts
export interface isDeactivatable {
canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}
离开-page.guard.ts
export class LeavePageGuard implements CanDeactivate<isDeactivatable>{
canDeactivate(
component: isDeactivatable
): Observable<boolean> | Promise<boolean> | boolean {
console.log('LeavePageGuard');
return component.canDeactivate();
}
}
测试-page.ts
export class TestPage implements OnInit, isDeactivatable{
canDeactivate(): Observable<boolean> | Promise<boolean> | boolean {
console.log('canDeactivate in TestPage');
return true;
}
}
首页-routing.Module.ts
const routes: Routes = [
{
path: '',
component: HomePage,
children:[
{
path: 'test',
loadChildren: () => import('../pages/test/test.module').then( m => m.TestPageModule),
canDeactivate: [LeavePageGuard]
}
]
}
]
离开 TestPage
时出现以下错误。
我尝试在 App 模块和 TestPage 模块中添加 LeavePageGuard
作为提供者,但仍然出现相同的错误。
core.js:6014 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'canDeactivate' of null
TypeError: Cannot read property 'canDeactivate' of null
at LeavePageGuard.canDeactivate (leave-page.guard.ts:15)
at router.js:5543
at Array.map (<anonymous>)
at runCanDeactivate (router.js:5532)
at MergeMapSubscriber.project (router.js:5349)
at MergeMapSubscriber._tryNext (mergeMap.js:46)
at MergeMapSubscriber._next (mergeMap.js:36)
at MergeMapSubscriber.next (Subscriber.js:49)
at Observable._subscribe (subscribeToArray.js:3)
at Observable._trySubscribe (Observable.js:42)
at resolvePromise (zone-evergreen.js:797)
at resolvePromise (zone-evergreen.js:754)
at zone-evergreen.js:858
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:39680)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559)
at invokeTask (zone-evergreen.js:469)
at ZoneTask.invoke (zone-evergreen.js:454)
解决方案非常简单,我找到了
我将 canDeativate 守卫从应用程序路由移动到该特定页面的模块路由。工作得很好。
测试-rouring.module.ts
const routes: Routes = [
{
path: '',
component: TestPage
canDeactivate: [ LeavePageGuard ]
}