91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

岳普湖县| 阿巴嘎旗| 德钦县| 清新县| 邹平县| 濮阳市| 秦皇岛市| 哈巴河县| 淄博市| 靖江市| 溆浦县| 宾川县| 江阴市| 汝州市| 攀枝花市| 恭城| 宕昌县| 卢龙县| 荥阳市| 泸溪县| 渑池县| 宜州市| 宁津县| 兰考县| 阿荣旗| 古交市| 镇原县| 施秉县| 三门县| 临海市| 西安市| 项城市| 和顺县| 黔江区| 通化市| 琼海市| 满洲里市| 青海省| 太湖县| 绵竹市| 城步|