最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue組件簡易模擬實(shí)現(xiàn)購物車

 更新時(shí)間:2020年12月21日 13:15:54   作者:吃不胖的貓o(=^ェ^=)m  
這篇文章主要為大家詳細(xì)介紹了Vue組件簡易模擬實(shí)現(xiàn)購物車,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Vue組件模擬實(shí)現(xiàn)購物車的具體代碼,供大家參考,具體內(nèi)容如下

代碼:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <script src="./lib/vue-2.4.0.js"></script>
 <style>
  #app{
   width:600px;
  }

  #myTable{
   width:500px;
   border-collapse:collapse;
  }

  td, th{
   text-align: center;
   font-size:20px;
   border:2px solid black;
  }

  td{
   height: 40px;
  }

  input{
   width: 30px;
   text-align: center   
  }

 </style>
</head>
<body>

 <div id="app">
  <my-cart></my-cart>
 </div>

 <script>
  var MyCommmodity = {
   props: ["list"],
   template:`
    <div>
     <button @click="baicai">白菜</button>
     <button @click="qingcai">青菜</button>
     <button @click="luobo">蘿卜</button> 
    </div>
   `,
   methods: {
    baicai: function(){
     var cai = {};
     cai.id = 4;
     cai.name = "白菜"
     cai.price = 3;
     cai.num = 1;
     this.list.push(cai)
    },
    qingcai: function(){
     var cai = {};
     cai.id = 5;
     cai.name = "青菜"
     cai.price = 6;
     cai.num = 1;
     this.list.push(cai)
    },
    luobo: function(){
     var cai = {};
     cai.id = 6;
     cai.name = "蘿卜"
     cai.price = 8;
     cai.num = 1;
     this.list.push(cai)
    }
   }
  }

  var MyTable = {
   props: ["list", "flag"],
   template:`
    <table id="myTable">
    <tr>
     <th>編號</th>
     <th>名稱</th>
     <th>單價(jià)</th>
     <th>數(shù)量</th>
     <th>操作</th>
    </tr>
    <tr :key="item.id" v-for="item in list"> 
     <td>{{item.id}}</td>
     <td>{{item.name}}</td>
     <td>{{item.price}}</td>
     <td>
      <button :disabled="flag" @click="sub(item.id)">-</button>
      <input type="text" :value="item.num" @blur="changeNum(item.id,$event)">
      <button @click="add(item.id)">+</button>
     </td>
     <td>
      <button @click="del(item.id)">刪除</button>
     </td>
    </tr>
   </table>
   `,
   methods: {
    changeNum: function(id, event){
     this.$emit("change-num",{
      id: id,
      type: "change",
      num: event.target.value
     });
    },
    sub: function(id){
     this.$emit("change-num",{
      id: id,
      type: "sub"
     })
    },
    add: function(id){
     this.$emit("change-num",{
      id: id,
      type: "add"
     })
    },    
    del: function(id){
     // alert(id);
     this.$emit("del-cart",id)
    }
   }
  }

  var MyPrice = {
   props: ["list"],
   template:`
    <div>
     <span>結(jié)算:</span>
     <span>{{total}}</span>     
    </div>
   `,
   computed: {
    total: function(){
     var t = 0;
     this.list.forEach(item => {
      t += item.price * item.num;
     });
     return t;
    }
   }
  }

  Vue.component('my-cart', {
   data () {
    return {
     flag:false,
     list:[{
      id: 1,
      name: "豬",
      price: "10",
      num:1,
     },
     {
      id: 2,
      name: "牛",
      price: "11",
      num:1,
     },
     {
      id: 3,
      name: "雞",
      price: "13",
      num:1,
     }]
    }
   },
   template:`
    <div> 
     <my-commmodity :list="list"></my-commmodity> 
     <my-table :list="list" :flag="flag" @change-num="changeNum($event)" @del-cart="delCart($event)"></my-table>
     <my-price :list="list"></my-price>
        
    </div>
   `,
   components:{ 
    'my-table':MyTable,
    'my-price':MyPrice,
    'my-commmodity':MyCommmodity,
   },
   methods:{
    changeNum: function(val){
     if(val.type ==="change"){
      this.list.some(item=>{
       if(item.id == val.id){
        item.num = val.num;
        return true;
       }
      });      
     }else if(val.type ==="sub"){
      this.list.some(item=>{
       if(item.id == val.id && item.num >0){
        item.num -= 1;
        return true;
       }
      }); 
     }else if(val.type ==="add"){
      this.list.some(item=>{
       if(item.id == val.id){
        item.num += 1;
        return true;
       }
      }); 
     }

    },
    delCart: function(id){
     var index = this.list.findIndex(item=>{
      return item.id == id;
     })
     this.list.splice(index,1)
    }
   }
  })


 var vm = new Vue({
  el: '#app',
  data:{
  }
 })
 </script>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue.js中 v-model 指令的修飾符詳解

    Vue.js中 v-model 指令的修飾符詳解

    v-model 指令默認(rèn)會(huì)在 input 事件中加載輸入框中的數(shù)據(jù)(中文輸入法中輸入拼音的過程除外)。這篇文章通過實(shí)例代碼給大家介紹Vue.js中 v-model 指令的修飾,感興趣的朋友跟隨小編一起看看吧
    2018-12-12
  • Nuxt的路由配置和參數(shù)傳遞方式

    Nuxt的路由配置和參數(shù)傳遞方式

    這篇文章主要介紹了Nuxt的路由配置和參數(shù)傳遞方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Vue打包后出現(xiàn)一些map文件的解決方法

    Vue打包后出現(xiàn)一些map文件的解決方法

    本篇文章主要介紹了Vue打包后出現(xiàn)一些map文件的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02
  • vue+elementUI的表格最后一行合計(jì)自定義顯示方式

    vue+elementUI的表格最后一行合計(jì)自定義顯示方式

    這篇文章主要介紹了vue+elementUI的表格最后一行合計(jì)自定義顯示方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 在vue項(xiàng)目中利用popstate處理頁面返回的操作介紹

    在vue項(xiàng)目中利用popstate處理頁面返回的操作介紹

    這篇文章主要介紹了在vue項(xiàng)目中利用popstate處理頁面返回的操作介紹,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • vue.js實(shí)現(xiàn)的幻燈片功能示例

    vue.js實(shí)現(xiàn)的幻燈片功能示例

    這篇文章主要介紹了vue.js實(shí)現(xiàn)的幻燈片功能,結(jié)合實(shí)例形式分析了vue.js實(shí)現(xiàn)幻燈片的相關(guān)樣式、配置、功能等操作技巧,需要的朋友可以參考下
    2019-01-01
  • vue3+vite項(xiàng)目跨域配置踩坑實(shí)戰(zhàn)篇

    vue3+vite項(xiàng)目跨域配置踩坑實(shí)戰(zhàn)篇

    vue3是一個(gè)流行的前端框架,vite是一個(gè)快速的構(gòu)建工具,下面這篇文章主要給大家介紹了關(guān)于vue3+vite項(xiàng)目跨域配置踩坑實(shí)戰(zhàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • vue在IIS服務(wù)器部署后路由無法跳轉(zhuǎn)

    vue在IIS服務(wù)器部署后路由無法跳轉(zhuǎn)

    在IIS服務(wù)器上部署Vue項(xiàng)目時(shí),可能會(huì)遇到路由無法正常跳轉(zhuǎn)的問題,解決方法有兩種,下面就來具體介紹一下解決方法,感興趣的可以了解一下
    2024-10-10
  • vue項(xiàng)目中如何使用mock你知道嗎

    vue項(xiàng)目中如何使用mock你知道嗎

    這篇文章主要為大家介紹了vue項(xiàng)目如何使用mock,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • vue-cli中的webpack配置詳解

    vue-cli中的webpack配置詳解

    本篇文章主要介紹了vue-cli中的webpack配置詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論

汶川县| 绩溪县| 老河口市| 文水县| 张北县| 昭觉县| 肥东县| 洛宁县| 博湖县| 遵化市| 全南县| 永安市| 乌什县| 鄂尔多斯市| 贵德县| 息烽县| 阿拉尔市| 新蔡县| 屏山县| 陇南市| 迁西县| 丽水市| 黔西县| 瓦房店市| 浦城县| 岳阳县| 湾仔区| 庆城县| 东乡县| 仁化县| 林芝县| 九龙城区| 平和县| 沅江市| 安国市| 东乌珠穆沁旗| 左贡县| 磴口县| 天峨县| 行唐县| 栾川县|