2017-07-07 66 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]; 
相关问题