2017-04-03 138 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