中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

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

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

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

观塘区| 大渡口区| 桃源县| 贞丰县| 汕尾市| 太康县| 鄂伦春自治旗| 邳州市| 灵石县| 昌邑市| 江孜县| 大安市| 佛冈县| 息烽县| 河北省| 当涂县| 义乌市| 平江县| 营山县| 扎囊县| 元阳县| 丹巴县| 衡南县| 淮南市| 宜兰市| 金昌市| 永平县| 玛曲县| 邵阳市| 呼伦贝尔市| 新竹县| 郸城县| 裕民县| 宁都县| 寿阳县| 惠来县| 翼城县| 团风县| 兴仁县| 平山县| 长兴县|