欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue實現(xiàn)購物車實例代碼兩則

 更新時間:2020年05月30日 10:52:25   作者:若星  
這篇文章主要介紹了Vue實現(xiàn)購物車實例代碼,需要的朋友可以參考下

一、第一種比較簡單

效果圖

實現(xiàn)代碼:

<!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實現(xiàn)的簡單響應式購物車案例

實現(xiàn)結(jié)果

如上,所有書類數(shù)據(jù)存在數(shù)組里,遍歷顯示在表格中,點擊+和-可以實現(xiàn)數(shù)量和總價格的響應式變化,其中,減號到1時便添加了disabled類型,無法點擊。 價格顯示時通過過濾器顯示的,加上Z¥符號和兩位小數(shù)。項目結(jié)構(gòu)為三個文件。

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>購買數(shù)量</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: '網(wǎng)絡導論',
 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;
}

到此這篇關(guān)于Vue實現(xiàn)購物車實例代碼的文章就介紹到這了,更多相關(guān)Vue 購物車內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解析vue路由異步組件和懶加載案例

    解析vue路由異步組件和懶加載案例

    這篇文章主要介紹了解析vue路由異步組件和懶加載案例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • 從vue源碼解析Vue.set()和this.$set()

    從vue源碼解析Vue.set()和this.$set()

    這篇文章主要介紹了從vue源碼看Vue.set()和this.$set()的相關(guān)知識,我們先來從Vue提供的Vue.set()和this.$set()這兩個api看看它內(nèi)部是怎么實現(xiàn)的。感興趣的朋友跟隨小編一起看看吧
    2018-08-08
  • vue中Promise的使用方法詳情

    vue中Promise的使用方法詳情

    這篇文章主要介紹了vue中Promise的使用方法詳情,Promise可以說是異步編程的一種解決方法,主要是為了解決代碼亂的情景而出現(xiàn)的,下文介紹其具體用法,需要的小伙伴可以參考一下
    2022-03-03
  • 淺談 vue 中的 watcher

    淺談 vue 中的 watcher

    這篇文章主要介紹了vue 中的 watcher的相關(guān)資料,需要的朋友可以參考下
    2017-12-12
  • Vue插槽具體用法及實例分析

    Vue插槽具體用法及實例分析

    本文主要介紹了Vue框架中插槽的使用方法和應用場景。通過具體實例分析,詳細講解了插槽的具體用法,幫助讀者深入理解Vue中插槽的使用和實現(xiàn)方式
    2023-05-05
  • vue2從數(shù)據(jù)變化到視圖變化之nextTick使用詳解

    vue2從數(shù)據(jù)變化到視圖變化之nextTick使用詳解

    這篇文章主要為大家介紹了vue2從數(shù)據(jù)變化到視圖變化之nextTick使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • Vue2中無法檢測到數(shù)組變動的原因及解決

    Vue2中無法檢測到數(shù)組變動的原因及解決

    由于某些限制,vue2不能檢測到某些情況下數(shù)組的變動,本文就將具體講解這兩種限制的解決思路
    2021-06-06
  • 在vue中獲取wangeditor的html和text的操作

    在vue中獲取wangeditor的html和text的操作

    這篇文章主要介紹了在vue中獲取wangeditor的html和text的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • 淺析vue-router原理

    淺析vue-router原理

    這篇文章主要圍繞Vue的SPA單頁面設計展開。SPA(single page application):單一頁面應用程序,有且只有一個完整的頁面,對vue router原理感興趣的朋友跟隨小編一起看看吧
    2018-10-10
  • vue.js element-ui validate中代碼不執(zhí)行問題解決方法

    vue.js element-ui validate中代碼不執(zhí)行問題解決方法

    這篇文章主要介紹了vue.js element-ui validate中代碼不執(zhí)行問題解決方法,需要的朋友可以參考下
    2017-12-12

最新評論