3 回答

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
使用來(lái)自stewdebaker的@Hostlistener的示例確實(shí)運(yùn)行良好,但是我對(duì)其進(jìn)行了另一處更改,因?yàn)镮E和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 關(guān)注
- 878 瀏覽
添加回答
舉報(bào)