2016-11-09 70 views
1

在組件1,我ngOnit裏面,我訂閱這就要求一個DB的一些東西,服務方法。在1.5秒內加載我的頁面就好了。然後我瀏覽着網頁/組件2,其中有一個返回按鈕,其中導航我回查看1angular2快速導航路線檔用戶

backBtn(){  
     this.router.navigate(['conversationlist']) 
    } 

視圖1,然後再加載ngOnit用戶,或者至少應該。如果我在視圖之間慢慢導航,大多數情況都會發生。但是,如果我快速進入視圖1,然後backBtn(),則組件1的用戶方法將停止,並且需要大約20秒才能加載。這是我在視圖1 ngOnit中調用的方法。再次,如果我快速瀏覽頁面,此功能需要很長時間。但是,如果我從頁面1導航到第2頁,請等待幾秒鐘,然後返回其通常的正常狀態。爲什麼這

思想停滯不前了/或滯後這麼辛苦,當快速導航?

ngOnInit() { 


     this.getConvAndMessageSub = this._conversationListService.getConversationListAndMessages(0, listBatch).subscribe(
      data => { 
       if(data){ 

         for(let convo of data.conversationList) 
         { 
          this.conversationList.push(convo) 

         } 
      }, 
      error => { 
       console.log(error) 
      }) 
    } 

    ngOnDestroy(){ 
     this.getConvAndMessageSub.unsubscribe(); 
    } 

這裏是服務代碼:

getConversationListAndMessages(offset, batch){ 
     this.clearConversationList(); 

     const token = localStorage.getItem('token') ? '?token=' + localStorage.getItem('token') : ''; 
     return this._http.get('getConversationList/' + offset + '/' + batch + token) 
      .map(response => { 
       const convoList =   response.json().data.conversationList; 

       this.myUser = response.json().data.myUser; 
       let count = 0; 

       if(response.json().data.conversationList.length == 0){ 
        return {conversationList:'', myUser:this.myUser} 
       } 
       for(let convo of convoList){ 
         this.setupUserObjects(convo); 
         this.getMessages(convo._id, -1, 0).subscribe(
         data => { 

          convo.messages = data['messages']; 

          for(let message of data['messages']){ 
           if(message.subscribersSeen.indexOf(myUser.uid) == -1){ 
            convo.unreadCount++; 
           } 
          } 
         }, 
         error => { 
          console.log(error) 
         } 
        ); 

        this.conversationList.push(convo) 
        count++; 
        if(count == convoList.length){ 

         return {conversationList:this.conversationList, myUser:this.myUser} 
        } 
       } 


      }) 
      .catch(error => Observable.throw(error.json())); 

    } 
+0

聽起來很有趣。您是否嘗試刪除ngDestroy上的訂閱? – Fiddles

+0

也可能是有用的,看多一點「組件1」的 – Fiddles

+0

是的,我有更多的組件1信息更新的代碼,是的,我刪除訂閱的破壞 –

回答

0

調試很多之後,我發現我有一些嵌套的用戶的方法。我退訂了一些,但不是全部。當我確定我退訂組件更改後。這解決了這個問題。