溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Vuejs如何實現(xiàn)購物車功能

發(fā)布時間:2021-04-21 11:10:50 來源:億速云 閱讀:127 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)Vuejs如何實現(xiàn)購物車功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

js有什么特點

1、js屬于一種解釋性腳本語言;2、在絕大多數(shù)瀏覽器的支持下,js可以在多種平臺下運行,擁有著跨平臺特性;3、js屬于一種弱類型腳本語言,對使用的數(shù)據(jù)類型未做出嚴(yán)格的要求,能夠進(jìn)行類型轉(zhuǎn)換,簡單又容易上手;4、js語言安全性高,只能通過瀏覽器實現(xiàn)信息瀏覽或動態(tài)交互,從而有效地防止數(shù)據(jù)的丟失;5、基于對象的腳本語言,js不僅可以創(chuàng)建對象,也能使用現(xiàn)有的對象。

功能概述

學(xué)習(xí)了Vue.JS的一些基礎(chǔ)知識,現(xiàn)在利用指令、數(shù)據(jù)綁定這些基礎(chǔ)知識開發(fā)一個簡單的購物車功能。功能要點如下:
(1)展示商品的名稱、單價和數(shù)量;
(2)商品的數(shù)量可以增加和減少;
(3)購物車的總價要實時更新,即數(shù)量發(fā)生變動,總價也要相應(yīng)的改變;
(4)商品可以從購物車中移除;
(5)具有選擇功能,只計算選中的商品的總價。

上一張截圖,如下:

Vuejs如何實現(xiàn)購物車功能

代碼

代碼分成三部分,分別是HTML、JS、CSS。關(guān)鍵的是HTML和JS。

HTML

<!DOCTYPE html>
<html>
 <head>
 <meta charset="utf-8">
 <title>Vue 購物車</title>
 <script src="../js/vue.min.js"></script>
 <link href="../css/cart.css" rel="external nofollow" rel="stylesheet">
 </head>
 <body>
 <div id="app" v-cloak>
  <template v-if="list.length">
  <table>
   <thead>
   <tr>
    <th><input type="checkbox" v-on:click="swapCheck" v-model="checked"></th>
    <th>商品名稱</th>
    <th>商品單價</th>
    <th>商品數(shù)量</th>
    <th>操作</th>
   </tr>   
   </thead>
   <tbody>
   <tr v-for="(item,index) in list">
    <td><input type="checkbox" v-model="selectList" :id="item.id" :value="index" name="selectList" ></td>
    <td>{{ item.name }}</td>
    <td>{{ item.price }}</td>
    <td>
    <button @click="handleReduce(index)" :disabled="item.count === 1">-</button>
    {{ item.count }}
    <button @click="handleAdd(index)">+</button>
    </td>
    <td><button @click="handleRemove(index)">移除</button></td>
   </tr>
   </tbody>
  </table>
  <div>總價:¥ {{ totalPrice }}</div>
  </template>
  <div v-else>購物車為空!</div>
 </div>

 <script src="../js/cart.js"></script>
 </body>
</html>

JS

var app = new Vue({
 el:'#app',
 data:{
 list:[
  {
  id:1,
  name:'iPhone 8',
  price:8888,
  count:1
  },
  {
  id:2,
  name:'Huwei Mate10',
  price:6666,
  count:1
  },
  {
  id:3,
  name:'Lenovo',
  price:6588,
  count:1
  }
 ],
 selectList:[],
 checked:false
 },
 computed:{
 totalPrice:function(){
  var total = 0;
  for(var i = 0,len = this.selectList.length;i < len;i++){
  var index = this.selectList[i];
  var item = this.list[index];
  if(item){
   total += item.price * item.count;
  }
  else{
   continue;
  }

  }
  return total.toString().replace(/\B(?=(\d{3})+$)/g,',');
 }
 },
 methods:{
 handleReduce:function(index){
  var item = this.list[index];
  if(item.count < 2){
  return;
  }
  item.count--;
 },
 handleAdd:function(index){
  var item = this.list[index];
  item.count++;
 },
 handleRemove:function(index){
  this.list.splice(index,1);
 },
 swapCheck:function(){

  var selectList = document.getElementsByName('selectList');
  var len = selectList.length;
  if(this.checked){
  for(var i = 0;i < len;i++){
   var item = selectList[i];
   item.checked = false;
  }
  this.checked = false;
  this.selectList = [];
  }
  else{
  for(i = 0;i < len;i++){
   item = selectList[i];
   if(item.checked === false){
   item.checked = true;
   this.selectList.push(selectList[i].value);
   }
  }
  this.checked = true;

  }
 }
 }
});

CSS

[v-cloak]{
 display: none;
}

table{
 border: 1px solid black;
 border-collapse: collapse;
 border-spacing: 0;
 empty-cells: show;
}

th,td{
 padding: 8px 16px;
 border:1px solid black;
 text-align: center;
}

th{
 background-color: gray;
}

關(guān)于“Vuejs如何實現(xiàn)購物車功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI