亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

vue如何實現簡單的購物車功能

發布時間:2022-07-04 09:31:36 來源:億速云 閱讀:197 作者:iii 欄目:開發技術

這篇文章主要介紹“vue如何實現簡單的購物車功能”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“vue如何實現簡單的購物車功能”文章能幫助大家解決問題。

1.實現效果:

vue如何實現簡單的購物車功能

2.涉及到的知識點:

toFixed函數、過濾器、reduce高階函數、v-bind:disabled、v-if

3.代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>書籍購物車案例</title>
    <style>
        table {
            border: 1px solid #e9e9e9;
            border-collapse: collapse;
            border-spacing: 0;
        }
        th, td {
            padding: 8px 16px;
            border: 1px solid #e9e9e9;
            text-align: left;
        }
        th {
            background-color: #f7f7f7;
            color: #5c6b77;
            font-weight:600;
        }
    </style>
</head>
<body>
    <div id="app">
        <div v-if="books.length">
        <table>
            <thead>
                <tr>
                    <th></th>
                    <th>書籍名稱</th>
                    <th>出版日期</th>
                    <th>價格</th>
                    <th>購買數量</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item,index) in books">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.date}}</td>
                    <td>¥{{item.price | finalPrice}}</td>
                    <td>
                        <button @click="item.count--" :disabled="item.count <=1">-</button>
                        {{item.count}}
                        <button @click="item.count++">+</button>
                    </td>
                    <td><button @click="btndelete(index)">移除</button></td>
                </tr>
            </tbody>
        </table>
        <h3>總價格:{{sumPrice | finalPrice}}</h3>
    </div>
    <div v-else><h3>購物車為空</h3></div>
    </div>
    <script src="../../js/vue.js"></script>
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> -->
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                books: [
                    {
                        id: 1,
                        name: '《算法導論》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    },
                    {
                        id: 2,
                        name: '《算法導論》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    },
                    {
                        id: 3,
                        name: '《算法導論》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    },
                    {
                        id: 4,
                        name: '《算法導論》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    },
                    {
                        id: 5,
                        name: '《算法導論》',
                        date: '2006-9',
                        price: 85.00,
                        count:1
                    }
 
                ]
            },
            methods: {
                btndelete(index){
                    this.books.splice(index,1);
                }
            },
            filters: {
                finalPrice(price){
                    return '¥' + price.toFixed(2);
                }
            },
            computed: {
                sumPrice(){
                    // 計算價格法1:
                    // let sum = 0;
                    // for(let book of this.books) {
                    //     sum += book.price * book.count;
                    // }
                    // return  sum;
                    // 計算價格法2,使用reduce函數。
                    return this.books.reduce(((preValue,book)=>preValue + book.count * book.price),0);
                }
            }
        })
    </script>
</body>
</html>

關于“vue如何實現簡單的購物車功能”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節

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

vue
AI

龙岩市| 故城县| 闻喜县| 镇康县| 屯留县| 大洼县| 江达县| 凤冈县| 隆子县| 阆中市| 黑龙江省| 石台县| 青田县| 黔东| 旅游| 广安市| 商水县| 徐州市| 石柱| 衡山县| 安泽县| 牟定县| 交城县| 邹平县| 青龙| 津南区| 鄂托克前旗| 曲周县| 黑山县| 太康县| 宜都市| 仁布县| 湖南省| 惠安县| 宁津县| 栖霞市| 铜山县| 五指山市| 思南县| 龙泉市| 余庆县|