<template>
<div>
<h2>捐款金额</h2>
<input type="number" v-model="donationAmount" placeholder="请输入捐款金额">
<button @click="donate">捐款</button>
<h2>捐款列表</h2>
<ul>
<li v-for="(donation, index) in donationList" :key="index">
{{ donation }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
donationAmount: 0,
donationList: [
'第一个项目',
'第二个项目'
]
};
},
methods: {
donate() {
if (this.donationAmount > 0) {
this.donationList.push(this.donationAmount);
this.donationAmount = 0;
}
}
}
};
</script>
代码效果: