vue3.x案例 购物车

购物车:

<template>
  <div>
    <table>
        <caption><h1>购物车</h1></caption>
      <tr>
        <th></th>
        <th>编号</th>
        <th>商品名称</th>
        <th>商品价格</th>
        <th>购买数量</th>
        <th>操作</th>
      </tr>

      <tr v-for="item in cartlist" :key="item.id">
        <td><input type="checkbox" v-model="item.checkbox"></td>
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.price}}</td>

      <td>
        <button @click="item.count--" :disabled="item.count>=1">-</button>
        {{item.count}}
        <button @click="item.count++">+</button>
      </td>
     <td><a href="#" @click.prevent="del(index)">删除</a></td>
      </tr>
      <tr>
        <td colspan="3" align="right">总价</td>
        <td colspan="3">{{totalprice.toFixed(2)}}</td>
      </tr>
    </table>
  </div>
</template>
<script>

export default {
  name: 'app',
  data () {
    return {
      cartlist: [
        { id: 1, checkbox: true, name: '《细说PHP》', price: 10, count: 1 },
        { id: 2, checkbox: true, name: '《细说网页制作》', price: 10, count: 1 },
        { id: 3, checkbox: true, name: '《细说JavaScript语》', price: 10, count: 1 },
        { id: 4, checkbox: true, name: '《细说DOM和BOM》', price: 10, count: 1 },
        { id: 5, checkbox: true, name: '《细说Ajax与jQuery》', price: 10, count: 1 },
        { id: 6, checkbox: true, name: '《细说HTML5高级API》', price: 10, count: 1 }
      ]
    }
  },
  computed: {
    totalprice: {
      get () {
        let sum = 0
        for (let book of this.cartlist) {
          if (book.checkbox) {
            sum += book.price * book.count
          }
        }
        return sum
      }
    }
  },
  methods: {
    del (index) {
      this.cartlist.splice(index,1);
    }
  }
}
</script>
<style scoped>
table {
  width:608px;
  border:1px solid #888888;
  border-collapse:collapse;
}

td,th {
  border:1px solid #888888;
  padding: 10px;
}
</style>

效果:
在这里插入图片描述
删除的步骤:

 <td><a href="#" @click.prevent="del(index)">删除</a></td>

del (index) {
this.cartlist.splice(index,1);
}
< style scoped>使得组件的样式只在自己当前的组件中有效,而不能在子组件中有效
props的默认形式:props:[‘name1’,‘name2’,‘name3’…];
也可以指定类型:
props:{
msg:{
type:String
},
title{
type:String
},
article{
type:Array
}
},
父组件向子组件传数据:用props
子组件向父组件传数据:用$emit

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值