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

使用Vue2實現(xiàn)簡單的購物車功能(可直接使用)

 更新時間:2023年08月13日 10:19:45   作者:天秤座的碼農  
這篇文章主要給大家介紹了如何使用Vue2實現(xiàn)簡單的購物車功能,文中有相關的代碼示例,對我們的學習或工作有一定的幫助,需要的朋友可以參考下

vue2.0實例簡單購物車

頁面展示效果如下:?

該購物車實現(xiàn)了自動計算小計、總價。

代碼實現(xiàn)

<body>
    <div id="app">
        <div>
            <form action="">
                商品名稱:<input type="text" v-model="productName" name="productName">
                商品單價:<input type="text" v-model="productPrice" name="productPrice">
                <input type="button" value="添加商品" @click="addProduct">
            </form>
        </div>
        <ul>
            <li v-for="(pro,index) in productList"  :key="index">
                商品名稱: {{pro.productName}} ---------------- 商品單價: {{pro.productPrice}} 
                <button @click="addproToCart(index)">添加商品</button>
            </li>
        </ul>
        <cart :cartlist="cartList"></cart>
    </div>
    <template id="cartHtml">
        <div>
            <table border="1">
                <tr>
                    <td>商品</td>
                    <td>商品名稱</td>
                    <td>商品價格</td>
                    <td>商品數量</td>
                    <td>商品價格</td>
                </tr>
                <tr v-for="(pro,index) in cartlist" :key="index">
                    <td><input type="checkbox" v-model="pro.active"></td>
                    <td>{{pro.productName}}</td>
                    <td>{{pro.productPrice}}</td>
                    <td>
                        <button @click="reduceProNum(index)">-</button>
                        {{pro.productNum}}
                        <button @click="addProNum(index)">+</button>
                    </td>
                    <td>{{pro.productPrice * pro.productNum}}</td>
                </tr>
                <tr>
                    <td colspan="3">選中的商品:{{activeNum}}/{{cartlist.length}}</td>
                    <td colspan="2">商品總價:{{totalprice}}</td>
                </tr>
            </table>
        </div>
    </template>    
</body>

js代碼

    var cart = {
        template:'#cartHtml',
        props:['cartlist'],
        methods:{
            // 商品數量+1
            addProNum(index){
                let product = this.cartlist[index];
                product.productNum++
            },
            // 商品數量-1
            reduceProNum(index){
                let product = this.cartlist[index];
                // 判斷商品數量是否為1
                if(product.productNum==1){
                    this.cartlist.splice(index,1) // 為1,從數組中刪除
                }else{
                    product.productNum--
                }
            }
        },
        computed:{
            activeNum(){
                let activeProdctList = this.cartlist.filter(item=>{
                    return item.active
                })
                return activeProdctList.length
            },
            totalprice(){
                let result = 0;
                for(pro of this.cartlist){
                    if(pro.active){
                        result = result + pro.productPrice * pro.productNum
                    }
                }
                return result;
            }
        }
    }
    var app = new Vue({
        el:'#app',
        data(){
            return{
                productName:'',
                productPrice:0,
                productList:[],
                cartList:[]
            }
        },
        methods:{
            addProduct(){
                // todo 對新增的商品名稱和單價,進行驗證
                // 查找新增的商品是否存在于商品列表中,如果不在存在返回-1
                let findindex = this.productList.findIndex(item=>{
                    return item.productName==this.productName
                })
                if(findindex==-1){ // 判斷商品列表中是否存在新增商品
                    // 把新商品添加到商品列表中
                    this.productList.push({productName:this.productName,productPrice:this.productPrice})
                }else{
                    alert('此商品已經存在') // 商品已存在,給出提示
                }
            },
            // 添加商品到購物車,index是單擊商品的下標
            addproToCart(index){
                let newproduct = this.productList[index]; // 根據下標從商品列表中取出商品對象
                // 從購物車列表中查找,是否存在新的商品,如果查到返回購物車中的商品
                let product = this.cartList.find(item=>{
                    return item.productName==newproduct.productName
                })
                if(product){
                    // 如果有對應商品數量加1
                    product.productNum++
                }else{
                    // 沒有對應商品,添加新的商品到購物車
                    this.cartList.push({
                        productName:newproduct.productName,
                        productPrice:newproduct.productPrice,
                        productNum:1,
                        active:true
                    });
                }
                console.log(product);
            }
        },
        components:{
            cart
        }
    })

到此這篇關于使用Vue2實現(xiàn)簡單的購物車功能(可直接使用)的文章就介紹到這了,更多相關Vue2實現(xiàn)購物車功能內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue3中reactive數據被重新賦值后無法雙向綁定的解決

    vue3中reactive數據被重新賦值后無法雙向綁定的解決

    這篇文章主要介紹了vue3中reactive數據被重新賦值后無法雙向綁定的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 詳解vue中的動態(tài)組件component和keep-alive

    詳解vue中的動態(tài)組件component和keep-alive

    這篇文章主要介紹了詳解vue中的動態(tài)組件component和keep-alive的相關資料,這大家需要注意include屬性和exclude屬性只能用一個,不能同時使用,本文給大家介紹的非常詳細,需要的朋友參考下吧
    2023-11-11
  • 基于Vue實現(xiàn)圖書管理功能

    基于Vue實現(xiàn)圖書管理功能

    這篇文章主要為大家詳細介紹了基于Vue實現(xiàn)圖書管理功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • vue-resouce設置請求頭的三種方法

    vue-resouce設置請求頭的三種方法

    本篇文章主要介紹了vue-resouce設置請求頭的三種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • vue項目打包優(yōu)化方式(讓打包的js文件變小)

    vue項目打包優(yōu)化方式(讓打包的js文件變小)

    這篇文章主要介紹了vue項目打包優(yōu)化方式(讓打包的js文件變小),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vuex模塊獲取數據及方法的簡單示例

    vuex模塊獲取數據及方法的簡單示例

    Vuex是一個專為Vue.js應用程序開發(fā)的狀態(tài)管理模式,它采用集中式存儲管理應用的所有組件的狀態(tài),并以相應的規(guī)則保證狀態(tài)以一種可預測的方式發(fā)生變化,下面這篇文章主要給大家介紹了關于vuex模塊獲取數據及方法的相關資料,需要的朋友可以參考下
    2023-03-03
  • vue在外部方法給下拉框賦值后不顯示label的解決

    vue在外部方法給下拉框賦值后不顯示label的解決

    這篇文章主要介紹了vue在外部方法給下拉框賦值后不顯示label的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue 中 template 有且只能一個 root的原因解析(源碼分析)

    Vue 中 template 有且只能一個 root的原因解析(源碼分析)

    這篇文章主要介紹了Vue 中 template 有且只能一個 root的原因解析,本文從源碼角度分析給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Vue使用axios添加請求頭方式

    Vue使用axios添加請求頭方式

    這篇文章主要介紹了Vue使用axios添加請求頭方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • vue.js中window.onresize的超詳細使用方法

    vue.js中window.onresize的超詳細使用方法

    這篇文章主要給大家介紹了關于vue.js中window.onresize的超詳細使用方法,window.onresize 是直接給window的onresize屬性綁定事件,只能有一個,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-12-12

最新評論

洛川县| 治县。| 安顺市| 饶河县| 桑日县| 新沂市| 阿鲁科尔沁旗| 屏山县| 乌苏市| 洪洞县| 渭南市| 库车县| 分宜县| 张家口市| 凤庆县| 那曲县| 儋州市| 邵阳市| 泽州县| 顺昌县| 射洪县| 新巴尔虎左旗| 克山县| 桐梓县| 上虞市| 临清市| 策勒县| 化德县| 漳州市| 五华县| 南城县| 清徐县| 喀喇沁旗| 吴江市| 东兰县| 尚志市| 顺昌县| 黎城县| 巴青县| 大竹县| 霍山县|