3 回答

TA貢獻1772條經驗 獲得超8個贊
使用來自stewdebaker的@Hostlistener的示例確實運行良好,但是我對其進行了另一處更改,因為IE和Edge在MyComponent類上向最終用戶顯示了canDeactivate()方法返回的“ false”。
零件:
import {ComponentCanDeactivate} from "./pending-changes.guard";
import { Observable } from 'rxjs'; // add this line
export class MyComponent implements ComponentCanDeactivate {
canDeactivate(): Observable<boolean> | boolean {
// insert logic to check if there are pending changes here;
// returning true will navigate without confirmation
// returning false will show a confirm alert before navigating away
}
// @HostListener allows us to also guard against browser refresh, close, etc.
@HostListener('window:beforeunload', ['$event'])
unloadNotification($event: any) {
if (!this.canDeactivate()) {
$event.returnValue = "This message is displayed to the user in IE and Edge when they navigate without using Angular routing (type another URL/close the browser/etc)";
}
}
}
- 3 回答
- 0 關注
- 885 瀏覽
添加回答
舉報