我試圖在數據中初始化一個空數組,然後從服務器獲取JSON並填充它。Vue:自動填充Observer對象的空陣列
的問題是,數組總是有一個額外的觀察對象,所以當我登錄它,我看到這一點:
empty items array: [ob: Observer]
這裏是一個代碼摘錄:
data() {
return {
items: []
}
},
created() {
this.$http.get('/api/menus').then(function (response) {
console.log('items before', this.items); //THIS LOGS items before: [__ob__: Observer]
this.items = [].concat(response.body);
this.items.forEach(function (item) {
console.log('item', item);
item.$add('active', false);
item.tests.forEach(function (test) {
test.$add('active', false);
});
});
}).catch(function (err) {
console.error('err', err);
});
},
的問題是,當試圖添加一個新的屬性到數組中的對象我得到一個錯誤:
err TypeError: item.$add is not a function
當我調試我se發生它是因爲它將觀察者對象視爲數組的一部分。
這是正常的嗎?我應該檢查$ add是否存在?在視圖中渲染它時呢,Vue會忽略這個對象嗎?
我認爲這是正常的,因爲VueJS使用觀察者的反應性,所以數據對象中的每個項目都附加了observer.Check但是使用瀏覽器控制檯,不使用JsBin http://jsbin.com/qusugoyame/edit?html,js,console,output –
是的,所有正確的,但是有什麼問題? – euvl
什麼應該是$ add方法?它depracated使用$ set –