直播商城原始碼,vue製作簡易的購物車

zhibo系統開發發表於2022-07-01

直播商城原始碼,vue製作簡易的購物車

<!DOCTYPE html>
<html>
<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>Document</title>
    <script src="vue3.0.js"></script>  //引入vue.js包
    <style>
        body {
width: 600px;
}
table {
    border: 1px solid black;
}
table {
    width: 100%;
}
th {
    height: 50px;
}
th, td {
    border-bottom: 1px solid #ddd;
    text-align: center;
}
span {
float: right;
}
[v-cloak] {
display: none;
}
    </style>
</head>
<body>
    <div id="app">
        <table>
            <tr>
                <th>商品號</th>
                <th>商品</th>
                <th>單價</th>
                <th>數量</th>
                <th>金額</th>
                <th>操作</th>
            </tr>
            <tr v-for='(book,index) in books' :key="book.id">
                <td>{{book.id}}</td>
                <td>{{book.title}}</td>
                <td>{{book.price}}</td>
                <td>
                    <button :disabled="book.count===0" @click="book.count--">-</button>
                    {{book.count}}
                    <button @click="book.count++">+</button> 
                </td>
                <td>{{itemprice(book.price,book.count)}}</td>
                <td><button @click="deleteitem(index)">刪除</button></td>
            </tr>
            
        </table>
        <p>總價:¥{{totalprice}}</p>
    </div>
    <script>
        //vue3.0語法
        const vm=Vue.createApp({
            data(){
                return{
                    books:[{
                            id: 1,
                            title: 'Java無難事',
                            price: 188,
                            count: 1
                          },
                          {
                            id: 2,
                            title: 'C++深入詳解',
                            price: 168,
                            count: 1
                          },
                          {
                            id: 3,
                            title: 'Servlet/JSP深入詳解',
                            price: 139,
                            count: 1
                          }]
                        
                    }
                },
                methods:{
                    itemprice(price,count){
                        return price*count;
                    },
                    deleteitem(index){
                        this.books.splice(index,1);
                    }
                },
                computed:{
                    totalprice(){
                        let total=0;
                        for(let book of this.books)
                        {
                            total+=book.price*book.count;
                        }
                        return total;
                    }
 
                }
            }).mount("#app");
    </script>
</body>
</html>


以上就是 直播商城原始碼,vue製作簡易的購物車,更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2903826/,如需轉載,請註明出處,否則將追究法律責任。

相關文章