2017-04-03 93 views
1

我有以下vue2代碼Vue公司未反應數據變化

methods: { 
    open: function(conversation){ 
     conversation.loading = true; 
     this.$set(this, 'activeConversation', conversation); 
     this.$http.get('/conversation/messages', { 
      params: { 
       conversationId: conversation.id 
      } 
     }).then((response) => { 
      // this.$set(this.activeConversation, 'messages', response.data); // this was the first try 
      this.activeConversation.messages.push(response.data[0]); // this is the second try 
     }) 
    } 
} 

當我改變activeConversation模板反應,第一$集。但是當我更改activeConversation的消息屬性時,什麼都不會發生。 我看到vue devtools中的更改,但DOM中沒有任何更改。

沒有什麼變化,我的意思是v-for =「在activeConversation.messages中的消息」不會呈現任何東西。在我有activeConversation:null的數據中也沒有{{activeConversation}}

。作爲談話的我通過與ID,發送對象,消息

+0

是否'activeConversation'屬性在你的''數據屬性messages'()'組件的方法?如果沒有,請將其添加並重試。除此之外,您可以直接設置屬性而不是調用'$ set'。 –

+0

在你的'v-for'中有''key'這樣的'v-for =「消息在activeConversation.messages」:key =「message」'? – SLYcee

+0

no .. activeConversation = null默認 – John

回答

0

請加activeConversation數據對象,例如:

... 
data: function(){ 
    return { 
     activeConversation: {} 
    }; 
} 
... 
+0

我有它= null – John

+0

OP已經將它添加到數據對象:'this。$ set(this,'activeConversation',conversation);'。即使它沒有在Vue實例中設置,它仍然會在晚些時候被注入:https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats – Terry