亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在離開頁面之前警告用戶未保存的更改

在離開頁面之前警告用戶未保存的更改

慕俠2389804 2019-11-07 10:17:19
我想警告用戶未保存的更改,然后再離開我的angular 2應用程序的特定頁面。通常,我會使用window.onbeforeunload,但不適用于單頁應用程序。我發現在角度1中,您可以進入$locationChangeStart事件confirm為用戶拋出一個框,但是我沒有看到任何顯示如何使角度2正常工作的東西,或者該事件是否仍然存在。我也看到過為ag1提供功能的插件onbeforeunload,但同樣,我還沒有看到將其用于ag2的任何方式。我希望其他人找到了解決該問題的方法;兩種方法都可以很好地達到我的目的。
查看完整描述

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)";

    }

  }

}


查看完整回答
反對 回復 2019-11-07
  • 3 回答
  • 0 關注
  • 885 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號