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

使用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>商品數(shù)量</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:{
            // 商品數(shù)量+1
            addProNum(index){
                let product = this.cartlist[index];
                product.productNum++
            },
            // 商品數(shù)量-1
            reduceProNum(index){
                let product = this.cartlist[index];
                // 判斷商品數(shù)量是否為1
                if(product.productNum==1){
                    this.cartlist.splice(index,1) // 為1,從數(shù)組中刪除
                }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){
                    // 如果有對應商品數(shù)量加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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue組件實現(xiàn)可搜索下拉框擴展

    vue組件實現(xiàn)可搜索下拉框擴展

    這篇文章主要為大家詳細介紹了vue組件實現(xiàn)可搜索下拉框的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Vue3請求攔截器里如何配置token

    Vue3請求攔截器里如何配置token

    這篇文章主要介紹了Vue3請求攔截器里如何配置token,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Vue實現(xiàn)過渡效果的基本方法

    Vue實現(xiàn)過渡效果的基本方法

    Vue 提供了一個強大的過渡系統(tǒng),可以用于在進入、離開和列表渲染時添加各種動畫效果,這些過渡不僅能夠提升用戶體驗,還能使界面更加生動和吸引人,本文將介紹 Vue 中實現(xiàn)過渡效果的基本方法,并提供使用 setup 語法糖的代碼示例,需要的朋友可以參考下
    2024-09-09
  • vue 音樂App QQ音樂搜索列表最新接口跨域設置方法

    vue 音樂App QQ音樂搜索列表最新接口跨域設置方法

    這篇文章主要介紹了vue 音樂App QQ音樂搜索列表最新接口跨域設置方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • Vue3將虛擬節(jié)點渲染到網頁初次渲染詳解

    Vue3將虛擬節(jié)點渲染到網頁初次渲染詳解

    這篇文章主要為大家介紹了Vue3將虛擬節(jié)點渲染到網頁初次渲染詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • vue實現(xiàn)不定高虛擬列表的示例詳解

    vue實現(xiàn)不定高虛擬列表的示例詳解

    這篇文章主要為大家詳細介紹了在vue環(huán)境單頁面項目下,如何實現(xiàn)不定高虛擬列表,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-10-10
  • vue-dplayer視頻播放器組件的使用詳解

    vue-dplayer視頻播放器組件的使用詳解

    Vue-DPlayer是一個易于使用、高性能的基于Vue.js的視頻播放器組件,這篇文章將為大家詳細介紹一下vue-dplayer視頻播放器組件的安裝與使用,需要的小伙伴可以參考下
    2023-09-09
  • 一文帶你了解Vue中單文件組件的使用

    一文帶你了解Vue中單文件組件的使用

    在web開發(fā)中,組件化開發(fā)已成為一種趨勢,Vue提供了一種高效的方式來創(chuàng)建和管理這些組件—單文件組件,下面我們就來看看它的具體應用吧
    2024-03-03
  • Vue3封裝ant-design-vue表格的詳細代碼

    Vue3封裝ant-design-vue表格的詳細代碼

    這篇文章主要介紹了Vue3封裝ant-design-vue表格,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-05-05
  • vue2.0組件之間傳值、通信的多種方式(干貨)

    vue2.0組件之間傳值、通信的多種方式(干貨)

    這篇文章主要介紹了vue2.0組件之間傳值、通信的多種方式以及注意要點,需要的朋友可以參考下
    2018-02-02

最新評論