简体   繁体   English

Angular 8 中的页面导航不会触发 canDeactivate()

[英]canDeactivate() is not triggered on page navigation in Angular 8

I have a requirement of throwing a popup to the user when navigating to different component from the current component.当从当前组件导航到不同的组件时,我需要向用户抛出一个弹出窗口。

And based on the input from the user i will have to either have to allow him to navigate away or make him stay.并且根据用户的输入,我将不得不要么让他离开,要么让他留下。

After a lot of googling i have found out that using a route guard which implements canDeactivate interface is the best approach to this problem.经过大量谷歌搜索后,我发现使用实现 canDeactivate 接口的路由保护是解决此问题的最佳方法。

However even after following a step-by-step process of implementing the guard, the canDeactivate() method implemented in the guard is never triggered on any of the navigations away from my component.然而,即使在遵循实现防护的分步过程之后,防护中实现的 canDeactivate() 方法也永远不会在离开我的组件的任何导航上触发。

Here is my guard which i have implemented as a service.这是我作为服务实施的警卫。

 import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, RouterStateSnapshot, CanDeactivate } from '@angular/router'; import { Observable } from 'rxjs'; import { SearchProjectComponent } from 'src/app/Project/search-project/search-project.component'; @Injectable({ providedIn: 'root' }) export class CanDeactivateGuard implements CanDeactivate<SearchProjectComponent> { canDeactivate(component: SearchProjectComponent, route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {debugger alert('called from guard'); const url: string = state.url; console.log('Url: ' + url); return component.canDeactivate ? component.canDeactivate() : true; } }

SearchComponent is my component for which i am trying to implement the Guard. SearchComponent 是我试图为其实现 Guard 的组件。 (On navigating away from this component, i want to throw a popup prompt) (离开这个组件时,我想抛出一个弹出提示)

Here is my AppRoutingModule这是我的 AppRoutingModule

 import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { AppComponent } from './app.component'; import { AuthGuard } from './shared'; import { CanDeactivateGuard } from './shared/services/can-deactivate-guard.service'; const routes: Routes = [ { path: '', loadChildren: () => import('./layout/layout.module').then(m => m.LayoutModule), canActivate: [AuthGuard] }, { path: 'login', loadChildren: () => import('./login/login.module').then(m => m.LoginModule) }, { path: 'access-denied', loadChildren: () => import('./access-denied/access-denied.module').then(m => m.AccessDeniedModule) }, { path: 'add-project', loadChildren: () => import('./Project/add-project/add-project.module').then(m => m.AddProjectModule) }, // tslint:disable-next-line:max-line-length { path: 'search-project', loadChildren: () => import('./Project/search-project/search-project.module').then(m => m.SearchProjectModule), canDeactivate: [CanDeactivateGuard] }, { path: '**', redirectTo: 'access-denied' }, ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {}

On navigating to any other component from searchcomponent, the canDeactivate method doesn't even get triggered.从 searchcomponent 导航到任何其他组件时,甚至不会触发 canDeactivate 方法。

Here is the code in my component.这是我的组件中的代码。

 canDeactivate(): Observable<boolean> | boolean {debugger alert('called form component'); if (this.registerForm.dirty) {debugger return false; //return this.dialogService.confirm('Discard changes for Person?'); } return true; }
Even the debugger in my component doesn't get triggered. 即使我的组件中的调试器也不会被触发。

Can someone help me with what i need to correct?有人可以帮助我解决我需要纠正的问题吗? (I am just a beginner in Angular, but i feel if this issue has something to do with the loadchildren() in the routingmodule) (我只是 Angular 的初学者,但我觉得这个问题是否与路由模块中的 loadchildren() 有关系)

You're missing to implement the component.canDeactivate inside your component, which is the only one who knows if the user can leave out or not.您缺少在component.canDeactivate内部实现 component.canDeactivate ,这是唯一知道用户是否可以省略的组件。

So, inside your components, implements this:所以,在你的组件中,实现这个:

canDeactivate(): boolean {
 return //condition to verify;
}

For more info, look here:欲了解更多信息,请看这里:

https://angular.io/guide/router#candeactivate-handling-unsaved-changes https://angular.io/guide/router#candeactivate-handling-unsaved-changes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM