在延迟加载的模块中定义一个特殊的组件,然后把CanDeactivate守卫放在这个组件的路由上。下面是代码示例:
import { Component } from '@angular/core';
@Component({
selector: 'app-lazy',
template: '
import { NgModule } from '@angular/core'; import { Routes, RouterModule } from '@angular/router'; import { LazyComponent } from './lazy.component';
const routes: Routes = [ { path: '', component: LazyComponent, canActivate: [], canDeactivate: [CanDeactivateGuard], children: [ { path: '', loadChildren: './lazy-child/lazy-child.module#LazyChildModule' } ] } ];
@NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule] }) export class LazyRoutingModule {}
import { Injectable } from '@angular/core'; import { CanDeactivate } from '@angular/router'; import { Observable } from 'rxjs/Observable';
@Injectable()
export class CanDeactivateGuard implements CanDeactivate