2016-10-13 64 views
3

我有一個孩子的組件,有以下方法:Vue.js訪問父組件

getSubscriptions() { 
    MessageService.subscriptions() 
      this.$parent.loading = true; 
      .then((data) => { 
       if (data.data.subscriptions == null) { 
        this.noSubscriptions = true; 
       } else { 
        this.subscriptions = data.data.subscriptions; 
       } 

       this.$parent.loading = false; 
      }).bind(this); 
} 

所以我想說明的負載循環在我父組件訪問它是這樣的:

this.$parent.loading 

(我的父組件有一個名爲loading數據屬性)

但是,當我嘗試編譯與一飲而盡^我得到:

[14:52:56] Starting 'browserify'... 
    46 |        } 
    47 | 
> 48 | t       this.$parent.loading = false; 
    |       ^
    49 |       }).bind(this); 
    50 |    }, 
    51 | 
{ SyntaxError: unknown: Unexpected token (48:28) 
    46 |        } 
    47 | 
> 48 | t       this.$parent.loading = false; 
    |       ^
    49 |       }).bind(this); 
    50 |    }, 

這裏有什麼問題?

(我使用Vue.js 1.0

回答

3

你把一個新的聲明中承諾設置的中間。您需要重新排列代碼:

this.$parent.loading = true; 
MessageService.subscriptions() 
     .then((data) => { 

但是,我不會直接訪問這樣的父項。它在父母和孩子之間創建了一個緊密的耦合 - 這個組件僅在父母暴露loading標誌時才起作用。請致電custom events