Este es un carrito de compras simple escrito de acuerdo con el video del Sr. Qianfeng Kerwin. Está escrito en vue3 y el software usa vscode. Para recursos de video detallados, vea el enlace
Introduzca la dirección de vue3
<script type="text/javascript" src="https://unpkg.com/vue@next"> </script>
Ajustar el formato de página general
<style>
/* 使得能够排在一行上 */
li {
display:flex;
justify-content:space-around;
padding:10px;
}
li img{
width:100px;
}
</style>
La imagen proviene de Baidu.
pic:"https://ts1.cn.mm.bing.net/th/id/R-C.0c8bf36e099654aadaf5f127ef1a3f1b?rik=uHrB%2blGez03%2fAA&riu=http%3a%2f%2fi3.img.969g.com%2fdown%2fimgx2014%2f10%2f24%2f289_102445_a1cff.jpg&ehk=EeF%2fioqRM6NfQqkCgXw%2bwLvO1%2fxZgeZ2pof7ALNLGsg%3d&risl=&pid=ImgRaw&r=0"
Calcula la cantidad total con el método sum()
sum(){
var total = 0
// 累加计算checkList数组的每一项的 价格*数量
this.checkList.forEach(item=>{
total+=item.price*item.number
})
return total
},
eliminación de controles
handleDeleteClick(index,id){
// console.log(index)
//删除的是datalist功能 靠index
this.datalist.splice(index,1)
//删除checklist 靠id
// console.log(id)
this.checkList = this.checkList.filter(item=>item.id!==id)
//为删除同步一下状态
this.handleItemChecked()
},
escribir seleccionar todo el método
handleAllCheck(){
// console.log(this.isAll)
if(this.isAll){
this.checkList = this.datalist
}else{
this.checkList=[]
}
},
codigo final
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
<style>
/* 使得能够排在一行上 */
li {
display:flex;
justify-content:space-around;
padding:10px;
}
li img{
width:100px;
}
</style>
<script type="text/javascript" src="https://unpkg.com/vue@next"> </script>
</head>
<body>
<div id="box">
<input type = "checkbox" v-model="isAll" @change="handleAllCheck">全选/全不选
<ul>
<li v-for="(item,index) in datalist" :key="item.id">
<input type="checkbox" v-model="checkList" :value="item" @change="handleItemChecked">
<img :src="item.pic">
<div>
<div>{
{item.name}}</div>
<div style="color:red;">${
{item.price}}</div>
</div>
<div>
<button @click="item.number--" :disabled="item.number===1">-</button>
<span>{
{item.number}}</span>
<button @click="item.number++" :disabled="item.number===item.limit">+</button>
</div>
<div>
<button @click="handleDeleteClick(index,item.id)">删除</button>
</div>
</li>
</ul>
<div>总金额:{
{sum()}}</div>
{
{checkList}}
</div>
<script type="text/javascript">
var obj={
data(){
return {
isAll:false,
checkList:[], //勾选的购车的数据
datalist: [{
name: "商品1",
price:10,
number:1,
id:1,
limit:5,
pic:"https://ts1.cn.mm.bing.net/th/id/R-C.0c8bf36e099654aadaf5f127ef1a3f1b?rik=uHrB%2blGez03%2fAA&riu=http%3a%2f%2fi3.img.969g.com%2fdown%2fimgx2014%2f10%2f24%2f289_102445_a1cff.jpg&ehk=EeF%2fioqRM6NfQqkCgXw%2bwLvO1%2fxZgeZ2pof7ALNLGsg%3d&risl=&pid=ImgRaw&r=0"
},
{
name: "商品2",
price:20,
number:2,
id:2,
limit:15,
pic:"https://ts1.cn.mm.bing.net/th/id/R-C.0c8bf36e099654aadaf5f127ef1a3f1b?rik=uHrB%2blGez03%2fAA&riu=http%3a%2f%2fi3.img.969g.com%2fdown%2fimgx2014%2f10%2f24%2f289_102445_a1cff.jpg&ehk=EeF%2fioqRM6NfQqkCgXw%2bwLvO1%2fxZgeZ2pof7ALNLGsg%3d&risl=&pid=ImgRaw&r=0"
},
{
name: "商品3",
price:30,
number:3,
id:3,
limit:15,
pic:"https://ts1.cn.mm.bing.net/th/id/R-C.0c8bf36e099654aadaf5f127ef1a3f1b?rik=uHrB%2blGez03%2fAA&riu=http%3a%2f%2fi3.img.969g.com%2fdown%2fimgx2014%2f10%2f24%2f289_102445_a1cff.jpg&ehk=EeF%2fioqRM6NfQqkCgXw%2bwLvO1%2fxZgeZ2pof7ALNLGsg%3d&risl=&pid=ImgRaw&r=0"
},
]
}
},
methods:{
sum(){
var total = 0
// 累加计算checkList数组的每一项的 价格*数量
this.checkList.forEach(item=>{
total+=item.price*item.number
})
return total
},
handleDeleteClick(index,id){
// console.log(index)
//删除的是datalist功能 靠index
this.datalist.splice(index,1)
//删除checklist 靠id
// console.log(id)
this.checkList = this.checkList.filter(item=>item.id!==id)
//为删除同步一下状态
this.handleItemChecked()
},
//全选方法
handleAllCheck(){
// console.log(this.isAll)
if(this.isAll){
this.checkList = this.datalist
}else{
this.checkList=[]
}
},
//每项选择
handleItemChecked(){
if(this.checkList.length===this.datalist.length){
// console.log("全选")
this.isAll = true;
}
else{
// console.log("未全选")
this.isAll = false;
}
}
}
}
var vm = Vue.createApp(obj).mount("#box")
</script>
</body>
</html>
Este es el código de imitación de aprendizaje, consulte el video escrito por el Sr. Qianfeng kerwin, el enlace es: [Tutorial del proyecto de desarrollo front-end web Qianfeng_1000 episodios desde cero introducción básica HTML5 + CSS3 + JS a la competencia (los datos incluyen aprendizaje front-end hoja de ruta)-beep Bilibili] https://b23.tv/dJqyfv1