当我单击@addToChart按钮时,我想进行{{total}}更新,它会从vue数组获取游戏价格。
HTML:
<p> Total Price: {{ total }} USD</p>
<button @click="addToChart" :disabled="!game.inStock"
:class="{ disabledButton: !game.inStock }">
Add to Chart
</button>
Vue:
el: "#app",
data: {
title: "",
about: "",
games: [{
id: 0,
title: "",
inStock: true,
price: 59,
},
{
id: 1,
title: "",
inStock: true,
price: 40,
},
methods: {
addToChart: function () {
this.cart += 1;
return total;
}
答案 0 :(得分:0)
您需要首先在数据中定义total
属性。
然后只需将价格作为您的addToCart
函数的参数即可。
new Vue({
el: "#app",
data: {
title: "",
about: "",
games: [
{ id: 0, title: "Game 1", inStock: true, price: 59 },
{ id: 1, title: "Game 2", inStock: true, price: 40 }
],
total: 0
},
methods: {
addToChart: function (price) {
this.total += price
}
}
})
<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
<div id="app">
<p> Total Price: {{ total }} USD</p>
<div v-for="game in games">
{{ game.title }}
<button @click="addToChart(game.price)" :disabled="!game.inStock">Add to Chart</button>
</div>
</div>