溫馨提示×

溫馨提示×

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

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

vue如果實現購物車功能

發布時間:2022-03-07 15:28:03 來源:億速云 閱讀:264 作者:iii 欄目:web開發

本文小編為大家詳細介紹“vue如果實現購物車功能”,內容詳細,步驟清晰,細節處理妥當,希望這篇“vue如果實現購物車功能”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

原理分析和實現

注意想實現該功能,需要學習:Vue學習之路(基礎篇),深入的了解每個指令的使用

首先,還是先把布局寫好,和引入vue,準備vue實例,這個不多說,代碼如下

<!DOCTYPE html>

<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">

<head>

    <meta charset="UTF-8">

    <title>vue實現購物車</title>

</head>

<body>

<div id="app">

    <h4>購物車</h4>

    名稱:<input style="width:60px" type="text" v-model="nameValue"> <br/>

    單價:<input style="width:60px" type="text" v-model="priceValue"> <br/>

    數量:<input style="width:60px" type="text" v-model="countValue">

    <button @click="add()">添加購物車</button>

    <hr/>

    <table border="1">

        <tr>

            <td>名稱</td>

            <td>單價</td>

            <td>數量</td>

            <td>小計</td>

        </tr>

        <tr v-for="(product,index) in products">

            <td>{{product.name}}</td>

            <td>{{product.price}}</td>

            <td>

                <button @click="desc(index)">-</button>

                {{product.count}}

                <button @click="incr(index)">+</button>

            </td>

            <td>{{product.price*product.count}}</td>

        </tr>

        <tr>

            <td colspan="4">總價:{{total()}}元</td>

        </tr>

    </table>

</div>

</body>

</html>

<script src="js/vue-min.js"></script>

<script>

    new Vue({

        el: "#app",

        data: {

            products: [

                {name: "秋褲", price: "81", count: 2},

                {name: "華為", price: "5810", count: 1},

            ],

            nameValue: "",

            priceValue: "",

            countValue: 0,

            totalPrice:0

        },

        methods: {

            incr(index) {

                this.products[index].count++;

            },

            desc(index) {

                this.products[index].count--;

            },

            add() {

                this.products.push({name: this.nameValue, price: this.priceValue, count: this.countValue});

                this.nameValue = "";

                this.priceValue = "";

                this.countValue = 0;

            },

            total(){

                var price=0;

                for (var i = 0; i <this.products.length; i++) {

                    price+=this.products[i].price * this.products[i].count

                }

                return price.toFixed(2);

            }

        }

    })

</script>

讀到這里,這篇“vue如果實現購物車功能”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

vue
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女