欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue实现购物车实例代码两则

程序员文章站 2022-07-06 17:37:00
一、第一种比较简单效果图实现代码:

一、第一种比较简单

效果图

Vue实现购物车实例代码两则

实现代码:

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="x-ua-compatible" content="ie=edge">
 <title>购物车案例</title>
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<style>
 *{
 padding: 0;
 margin:0
 }
 ul li{
 width: 1200px;
 display: flex;
 align-items: center;
 justify-content: center;
 }
 li div,.total{
 display: inline-block;
 width:200px;
 height: 50px;
 line-height: 50px;
 text-align: center;
 }
 button{
 width: 60px;
 height: 40px;
 text-align: center;
 line-height: 40px;
 }

</style>
<body>

 <div id="app">
 <ul>
  <goodsitem 
  v-for="item in goodslist" 
  :item="item" 
  :key="item.id"
  @onchange="(type)=>{handlecount(type,item)}"
  @ondelete="()=>{handledelete(item.id)}">
  </goodsitem>
  <div class="total" style="padding-left: 113px">总价:{{total}}</div>
 </ul>
 </div>

</body>
<script>
 var computed={
 props:{
  count:{
   type:number,
   require:true
  }
 },
 methods:{
  handlecount(type){
  this.$emit('onchange',type)
  }
 },
 template:`<div style="width:200px">
   <button @click="handlecount('sub')">-</button>
   <span>{{count}}</span>
   <button @click="handlecount('add')" >+</button>
   </div>
 
 `
 
 }
 var goodsitem={
 props:{
  item:{
  type:object,
  require:true
  }
 },
 methods:{
  handlecount(type){
  this.$emit('onchange',type)
  },
  handledelete(){
  this.$emit('ondelete')
  }
 },
 components:{
  computed
 },
 template:`<li>
   <div>{{item.goodsname}}</div>
   <div>{{item.price}}</div>
   <computed :count="item.count" @onchange="handlecount"></computed>
   <div>{{item.sum}}</div>
   <div><button @click="handledelete">删除</button></div>
   </li>
   `
 }

 var app=new vue({
 el:"#app",
 data:{
  goodslist:[{
  id:1,
  goodsname:"小可爱",
  price:100,
  count:1,
  sum:100
  },{
  id:2,
  goodsname:"小可爱",
  price:200,
  count:2,
  sum:400
  },{
  id:3,
  goodsname:"小可爱",
  price:300,
  count:3,
  sum:900
  },{
  id:4,
  goodsname:"小可爱",
  price:400,
  count:1,
  sum:400
  },
  ]
 },
 methods:{
  handlecount(type,item){
  if(type=='add'){
   item.count+=1
  }else{
   if(item.count==1){
   this.handledelete(item.id) 
   return
   }
   item.count-=1
  }
  item.sum=item.count*item.price
  },
  handledelete(id){
  return this.goodslist=this.goodslist.filter((item)=>{
   return id!=item.id
  })
  }
 },
 computed:{
  total(){
  return this.goodslist.reduce((total,item)=>{
   return total+=item.sum
  },0)
  }
 },
 components:{
  goodsitem
 }
 })
</script>
</html>

二、一个用vue实现的简单响应式购物车案例

实现结果

Vue实现购物车实例代码两则

如上,所有书类数据存在数组里,遍历显示在表格中,点击+和-可以实现数量和总价格的响应式变化,其中,减号到1时便添加了disabled类型,无法点击。 价格显示时通过过滤器显示的,加上z¥符号和两位小数。项目结构为三个文件。

index.html

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>title</title>
 <link rel="stylesheet" href="style.css" rel="external nofollow" >
</head>
<body>
<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="increment(index)" v-bind:disabled="item.count<=1">-</button>
 {{item.count}}
 <button @click="decrement(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 src="main.js"></script>
<script></script>
</body>
</html>

main.js

const app = new vue({
 el:"#app",
 data: {
 books: [
 {
 id: 1,
 name: '算法导论',
 date: '2019-01-10',
 price: 85.00,
 count: 1
 },
 {
 id: 2,
 name: '计算机导论',
 date: '2019-02-14',
 price: 90.00,
 count: 2
 },
 {
 id: 3,
 name: '科学导论',
 date: '2019-09-10',
 price: 85.21,
 count: 1
 },
 {
 id: 4,
 name: '网络导论',
 date: '2019-08-21',
 price: 19.02,
 count: 1
 },
 ]
 },
 methods:{
 getfinalprice(price) {
 return '$' + price.tofixed(2)
 },
 increment(index){
 /*if(this.books[index].count <= 1) return*/
 this.books[index].count--
 },
 decrement(index){
 this.books[index].count++
 },
 removehandler(index){
 this.books.splice(index,1)
 }
 },
 filters:{
 showprice(price){
 return '$' + price.tofixed(2)
 }
 },
 computed:{
 totalprice(){
 let tprice = 0
 for(let i = 0; i< this.books.length; i++)
 {
 tprice += this.books[i].price * this.books[i].count
 }
 return tprice
 }
 }
})

style.css

table{
 border: 1px solid #e9e9e9;
 border-collapse: collapse;
 bordre-spacing: 0;
}

th, td {
 padding: 8px 16px;
 border: 1px solid #e9e9e9;
 text-align: left;
}

th{
 backgroud-color: #f7f7f7;
 color: #5c6b77;
 font-weight: 600;
}

到此这篇关于vue实现购物车实例代码的文章就介绍到这了,更多相关vue 购物车内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: Vue 购物车