Vue組件簡(jiǎn)易模擬實(shí)現(xiàn)購(gòu)物車
更新時(shí)間:2020年12月21日 13:15:54 作者:吃不胖的貓o(=^ェ^=)m
這篇文章主要為大家詳細(xì)介紹了Vue組件簡(jiǎn)易模擬實(shí)現(xiàn)購(gòu)物車,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Vue組件模擬實(shí)現(xiàn)購(gòu)物車的具體代碼,供大家參考,具體內(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>編號(hào)</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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
npm install sentry-cli失敗的問(wèn)題解決
本文主要介紹了npm install sentry-cli失敗的問(wèn)題解決,文章首先描述了問(wèn)題現(xiàn)象,然后分析了問(wèn)題的原因,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08基于Vue實(shí)現(xiàn)簡(jiǎn)單的貪食蛇游戲
貪食蛇是一個(gè)非常經(jīng)典的游戲,?在游戲中,?玩家操控一條細(xì)長(zhǎng)的直線,?它會(huì)不停前進(jìn),?玩家只能操控蛇的頭部朝向,?一路拾起觸碰到之物。本文將用Vue實(shí)現(xiàn)這一游戲,感興趣的可以嘗試一下2022-04-04vue連接本地服務(wù)器的實(shí)現(xiàn)示例
本文主要介紹了vue連接本地服務(wù)器的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01vue父組件中獲取子組件中的數(shù)據(jù)(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇vue父組件中獲取子組件中的數(shù)據(jù)(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09vue打包生成的文件的js文件過(guò)大的優(yōu)化方式
這篇文章主要介紹了vue打包生成的文件的js文件過(guò)大的優(yōu)化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04