如果有任何更改,如何在离开 Angular 4 页面时提示用户?

iPa*_*aul 3 typescript primeng angular

我的Angular4 页面中有两个选项卡(PrimeNg Tabview),其中包含许多输入字段,例如textboxestextareas下拉列表等。我还有一个侧边菜单栏可以导航到应用程序中的其他页面。我想如果这些输入字段中的数据有任何更改,那么如果用户尝试更改选项卡或使用侧面导航到其他页面,则会出现一条弹出消息(模式或确认框)菜单栏。

ngOnDestroy()方法我试过了。虽然此方法仅在页面关闭时触发(通过导航到其他页面或移动到下一个标签页),但它不允许您从方法中显示模式/确认框,因为它处于销毁生命周期挂钩.

请帮助我确定实际事件,如果页面/表单中的任何输入字段发生更改,我可以使用该事件向用户提示确认框。

@surjit 建议:

my-guard.service.ts

export interface CanComponentDeactivate {
    CanDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

@Injectable()
export class CreateRequestGuardService implements CanDeactivate<CanComponentDeactivate> {
    canDeactivate(component: CanComponentDeactivate) {
        let vDeactivate = component.CanDeactivate ? component.CanDeactivate() : true;
        return vDeactivate;
    }   
}
Run Code Online (Sandbox Code Playgroud)

my-component.ts 中

import { CreateRequestGuardService } from './my-guard.service';

canDeactivate(): Observable<boolean> | boolean {
    console.log('I am moving away!');
    if (!this.isSaved) {
        const confirmation = window.confirm('Are you sure?');
        return Observable.of(confirmation);
    }
    else{
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

my-router.ts 中

import { CreateRequestGuardService } from './my-guard.service';

const allRoutes: Routes = [
   { path: 'save', component: my-component, canDeactivate: [MyGuardService]},
   { path ....},
   ..
]
Run Code Online (Sandbox Code Playgroud)

小智 6

CanDeactivate在要保护的组件路由中使用guard

阅读CanDeactivate了解更多信息

更新:这是我的伪代码代码。它没有经过测试

我的guard.service.ts:

export interface CanComponentDeactivate {
    CanDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

@Injectable()
export class CreateRequestGuardService implements CanDeactivate<CanComponentDeactivate> {
    canDeactivate(component: CanComponentDeactivate, 
                  currentRoute: ActivatedRouteSnapshot, 
                  currentState: RouterStateSnapshot, 
                  nextState?: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        return component.canDeactivate(); //the function will be called on the component, that's why we'll implement the function on the component.
    }   
}
Run Code Online (Sandbox Code Playgroud)

我的路由器.ts:

import { CreateRequestGuardService } from './my-guard.service'; //the class name in my-guard.service.ts

const allRoutes: Routes = [
   { path: 'save', component: myComponent, canDeactivate: [CreateRequestGuardService]},
   { path ....},
   ..
]
Run Code Online (Sandbox Code Playgroud)

我的component.ts

import { CreateRequestGuardService } from './my-guard.service';

export class myComponent implements CanComponentDeactivate { //implements the interface created on the guard service

    /*
        your component data
    */
    //implementation of canDeactivate
    canDeactivate(): Observable<boolean> | Promise<boolean> | boolean {
        if(form_is_edited && not_saved){
            return confirm("Discard Changes?");
        }
        else
            return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:也不要忘记将保护服务(类)添加到文件中的providers数组中app.module.ts