2017-07-07 61 views
1

我想在VueJs建立一個小型應用程序, 以下是我的數據集:在那裏我有在愛可信通話如何Object元素推到一個數組中Vuejs/JavaScript的

data(){ 
    return { 
     pusher: '', 
     channel:'', 
     notify: [], 
     notifications: '', 
     notificationsNumber: '', 
    } 
}, 

組件的創建屬性:

axios.get('api/notifications', {headers: getHeader()}).then(response => { 
    if(response.status === 200) 
    { 
     this.notify = response.data.notifications 
     this.notificationsNumber = this.notify.length 
    } 
}).catch(errors => { 
    console.log(errors); 
}) 

我有pusherJs實現的,所以我有下面的代碼:

this.pusher = new Pusher('xxxxxxxx', { 
    cluster: 'ap2', 
    encrypted: true 
}); 

var that = this 
this.channel = this.pusher.subscribe('stellar_task'); 
this.channel.bind('company_info', function(data) { 
    console.log(data.notification); 
    that.notifications = data.notification 
}); 

一旦被從推杆獲得的價值我想這推到我的陣列作爲通知手錶的財產,是這樣的:

watch: { 
    notifications(newValue) { 
     this.notify.push(newValue) 
     this.notificationsNumber = this.notificationsNumber + 1 
    } 
} 

所以,問題是,我通過推接收數據格式在對象的形式和推送功能未在此得到實施:

截圖:

Console log of pusher response

幫我解決這個問題。

+0

你能'的console.log(this.notify)'內'watch'?如果它是一個'array',它必須有'push'。 – shotor

+0

@morgh是對的,'this.notify'看起來不是一個數組。 – MatWaligora

+0

@MatWaligora我已經將它定義爲數據集中的數組,我不知道發生了什麼,但是在做console.log的時候我得到了答案。 –

回答

0

我在做一個假設,response.data.notifications是一個陣列像對象

因此,所有你需要做的就是:

this.notify = [...response.data.notifications]; 
相關問題