vue点击按钮添加组件,类似于添加购物车效果。
首先在template里
<template>
<div class="hello">
<header>
<div>
<input type="text" v-model="child" />
<button @click="addChild">点击添加儿童</button>
</div>
<div>
<input type="text" v-model="man" />
<button @click="addMan">点击添加成人</button>
</div>
<div>
<input type="text" v-model="old" />
<button @click="addOld">点击添加老人</button>
</div>
</header>
<section>
<ul>
<template v-for="(item,index) in list">
<span :key="index" v-if="item.type === 'child'">
<p>车票 + 儿童票</p>
<label for="">姓名:<input type="text"></label>
<br>
<label for="">手机号:<input type="text"></label>
<br>
<label for="">身份证:<input type="text"></label>
</span>
<span :key="index" v-else-if="item.type === 'man'">
<p>车票 + 成人票</p>
<label for="">姓名:<input type="text"></label>
<br>
<label for="">手机号:<input type="text"></label>
<br>
<label for="">身份证:<input type="text"></label>
</span>
<span :key="index" v-else>
<p>车票 + 老人票</p>
<label for="">姓名:<input type="text"></label>
<br>
<label for="">手机号:<input type="text"></label>
<br>
<label for="">身份证:<input type="text"></label>
</span>
</template>
</ul>
</section>
</div>
</template>
然后是script
<script>
export default {
name: 'HelloWorld',
data() {
return {
child: 0,
man: 0,
old: 0,
list: []
}
},
methods: {
addChild() {
this.child++
this.list.push({
type: 'child'
})
},
addMan() {
this.man++
this.list.push({
type: 'man'
})
},
addOld() {
this.old++
this.list.push({
type: 'old'
})
}
}
}
</script>
最后呢也就是一个简易的样式
<style scoped>
header div {
margin-top: 20px;
}
</style>
样式呢,自己调就好了。效果图是静态的我就不上传了,感兴趣的朋友可以运行起来看效果。
本文介绍了如何使用Vue.js实现一个功能,即点击按钮动态地添加组件,该效果类似于购物车的添加功能。在模板部分设置好结构,接着在script中编写逻辑,最后通过简单的CSS调整样式。虽然未提供实际效果图,但鼓励读者自行运行代码查看效果。
&spm=1001.2101.3001.5002&articleId=106528247&d=1&t=3&u=0190350444d9467d9e4ef0cf3221b28f)
6662

被折叠的 条评论
为什么被折叠?



