簡體   English   中英

有沒有辦法知道是否在角度2中未發生路線事件DID?

[英]Is there a way to know, whether a route event DID NOT occur in angular 2?

在代碼塊中,當發生路由器事件以及未發生路由器事件時,我必須有條件地運行一些語句。 我嘗試使用路由器訂閱變量的closed屬性。 那沒有用。 我想知道是否可以檢測到是否沒有路由器事件,因為如果沒有路由事件是同步的並且即使路由發生更改也要運行(因為這是異步的且幾乎沒有延遲),所以要運行的代碼。

.catch((error: any) => {
        if (this.routerSubscription)
            this.routerSubscription.unsubscribe();
        this.routerSubscription =  this._router.events.subscribe(
            (event) => {
                if (event instanceof NavigationEnd) {
                   // code if router event occurs
                    this.routerSubscription.unsubscribe();
                }
            }
        );

        // code if no router event occurs
      return  Observable.throw(error);
  });

您可以設置一個標志,以防發生路由器事件,並在完整的回調中檢查是否發生了:

    bool wasEvent = false;
    this.routerSubscription =  this._router.events
        .filter(e => event instanceof NavigationEnd)
        .first() // take only one
        .subscribe(
          event => wasEvent = true,
          (err) => {},
          () => console.log(`Router events happened: ${wasEvents}`)
        );            
    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM