如果使用頁面,我們有要求提醒用戶的位置,如果使用選擇不同的菜單應用程序,應該用Yes或Yes顯示提醒,如果用戶繼續進行應該發生重定向,否則應該回到同一頁面。我們嘗試過使用ngOnDestroy,但應用程序正在重定向到下一個頁面而沒有顯示Alert。Angular 2 - 如果觸摸頁面,則在離開頁面之前提醒用戶
我的第一種方法是:
ngOnDestroy()
{
this.touched = this.eventForm.touched;
if (this.touched)
this.display = true;
}
現在我用CanDeactivate
後衛(指this plunker爲例)嘗試:
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { SidebarComponent } from './shared/sidebar/sidebar.component';
@Injectable()
export class ConfirmDeactivateGuard implements CanDeactivate<SidebarComponent> {
canDeactivate(target: SidebarComponent) {
if (target.hasChanges()) {
return window.confirm('Do you really want to cancel?');
}
return true;
}
}
的可能的複製(https://stackoverflow.com/questions/35922071/warn-user-of- [警告未保存的更改用戶離開頁面之前] unsaved-changes-before-leaving-page) –
請檢查此鏈接及其下劃線的回購 https://rahulrsingh09.github.io/AngularConcepts/#/guard,我已經實施了can decativate警衛。 –