我在使用vuejs 1.0 $集()的麻煩,我已經做出了jsfiddle。當addQty1
工作並更新顯示,當addQty2
只更新數據。
任何想法爲什麼會發生這種情況?
var TicketLine = Vue.extend({
template: '#ticketLineTemplate',
props: ['index', 'lines'],
data: function() {
var line = this.lines[this.index];
return {
qty: line.qty,
label: line.label
};
},
methods: {
addQty1: function() {
var line = this.lines[this.index];
line.qty++;
this.lines.$set(this.index, $.extend({}, line));
},
addQty2: function() {
var line = this.lines[this.index];
line.qty++;
this.lines.$set(this.index, line);
}
}
});
var lines = [{
qty: 1,
label: 'Pizza'
}, {
qty: 2,
label: 'Café'
}, {
qty: 1,
label: 'Soda'
}];
new Vue({
el: "#app",
data: {
lines: lines
},
components: {
'ticket-line': TicketLine
}
});
<div id="app">
<div id="ticket">
<table class="table">
<thead>
<th>Qty</th>
<th>Label</th>
<th>Actions</th>
</thead>
<tbody>
<template v-for="line in lines">
<ticket-line :index="$index" :lines="lines"></ticket-line>
</template>
</tbody>
</table>
</div>
<script type="text/x-template" id="ticketLineTemplate">
<tr>
<td>{{ qty }}</td>
<td>{{ label }}</td>
<td>
<button type="button" v-on:click="addQty1()">addQty1</button>
<button type="button" v-on:click="addQty2()">addQty2</button>
</td>
</tr>
</script>
</div>
想過去對於那些出這個題目的範圍的具體原因都行,我讀的反應性的指南,你聯繫我,仍然沒有得到它,$組()被suposed觸發變化檢測。我錯過了什麼? – Nadjib
請參閱編輯 - 對象上的'$ set'不會觸發更改,但'qty'屬性上的'$ set'將 – Jeff
是的,它仍然無法工作,可能結構需要更改。如果沒有找到其他解決方案,將會推薦你,謝謝@Jeff – Nadjib