Vue案例:购物车

效果展示:

核心代码如下:

 <div id="app">
    <div v-if="books.length">
      <table>
        <thead>
          <tr>
            <th></th>
            <th>书籍名称</th>
            <th>出版日期</th>
            <th>价格</th>
            <th>购买数量</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item,index) in books">
            <!-- <td v-for="value in item">{{value}}</td> -->
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.date}}</td>
            <!-- <td>{{getFinalPrice(item.price)}}</td> -->
            <td>{{item.price | showPrice}}</td>
            <td>
              <button @click="decrement(index)" v-bind:disabled= "item.count <= 1">-</button>
              {{item.count}}
              <button @click="increment(index)">+</button>
            </td>
            <td><button @click="removeHandler(index)">移除</button></td>    
          </tr>
        </tbody>
      </table>
      <h2>总价格:{{totalPrice | showPrice}}</h2>
    </div>
    <h2 v-else>购物车已清空!</h2>
  </div>
  <script src="../js/vue.js"></script>
  <script>
  const app = new Vue({
    el:'#app',
    data:{
      isShow:true,
      books:[
        {
          id:1,
          name:'《算法导论》',
          date:'2006-9',
          price:85.00,
          count:1
        },
        {
          id:2,
          name:'《UNIX编程艺术》',
          date:'2006-2',
          price:59.00,
          count:1
        },
        {
          id:3,
          name:'《编程珠玑》',
          date:'2006-9',
          price:39.00,
          count:1
        },
        {
          id:4,
          name:'《代码大全》',
          date:'2008-10',
          price:128.00,
          count:1
        }
      ]
    },
    methods:{
      getFinalPrice(price){
        return '¥' + price.toFixed(2);
      },
      increment(index){
        this.books[index].count++;
      },
      decrement(index){   
        this.books[index].count--;
      },
      removeHandler(index){
        this.books.splice(index,1);
      }
    },
    computed:{
      totalPrice(){
        return this.books.reduce(function(preValue,book){
          return preValue + book.count * book.price
        },0)

      },

    },
    filters:{//过滤器
      showPrice(price){
        return '¥' + price.toFixed(2)
      }  
    }
  })
  </script>

免费下载连接: 购物车案例icon-default.png?t=M3C8https://download.csdn.net/download/A____t/85147759?spm=1001.2014.3001.5503

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js购物车案例是一个使用Vue.js框架实现的简单购物车功能的示例。在这个案例中,我们通过Vue实例化一个对象,并使用data属性来存储购物车中的商品信息,包括商品的id、名称、日期、价格和数量。使用methods属性来定义一些方法,例如减少商品数量、增加商品数量和删除商品等。同时,使用filters属性来定义一个过滤器,用于显示商品价格的格式。使用computed属性来计算购物车中选中商品的总价和数量。 在HTML模板中,我们可以通过v-for指令循环渲染出购物车中的每一个商品,通过v-bind指令将商品的属性绑定到相应的DOM元素上,通过v-on指令将事件与方法绑定在一起,实现交互功能。通过v-model指令实现双向数据绑定,使得商品数量的变化可以反映在数据中。 在Vue实例的computed属性中,我们使用filter方法筛选出被选中的商品,并使用reduce方法计算总价。 总的来说,Vue.js购物车案例通过Vue的数据绑定和计算属性的特性实现了一个简单的购物车功能,可以方便地对商品进行增加、减少和删除操作,并且实时计算选中商品的总价和数量。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Vue.js-购物车案例](https://blog.csdn.net/weixin_48137421/article/details/125110902)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Vue-购物车案例(非常详细)](https://blog.csdn.net/qq_38666686/article/details/122065543)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [vue整合SSM项目实战](https://download.csdn.net/download/m0_55755339/88241603)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值