前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue实战之建立购物车

Vue实战之建立购物车

作者头像
Dream城堡
发布2018-10-25 15:02:28
7120
发布2018-10-25 15:02:28
举报
文章被收录于专栏:Spring相关Spring相关Spring相关

Vue实战之建立购物车

1.效果演示:

cart.gif

2.相关代码:

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>购物车示例</title>
    <link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>

<div id="app" v-cloak>
    <template v-if="list.length">
        <table border="1px" style="border-collapse:collapse;">
            <thead>
            <tr>
                <th></th>
                <th>商品名称</th>
                <th>商品单价</th>
                <th>购买数量</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="(item,index) in list">
                <td>{{index+1}}</td>
                <td>{{item.name}}</td>
                <td>{{item.price}}</td>
                <td>
                    <button @click="handleReduce(index)"
                            :disabled="item.count ===1"
                    >-
                    </button>
                    {{item.count}}
                    <button @click="handleAdd(index)">+</button>
                </td>
                <td>
                    <button @click="handdleRemove(index)">移除</button>
                </td>
            </tr>
            </tbody>
        </table>
        <div>总价:&yen;{{totalPrice}}</div>
    </template>
    <div v-else>购物车为空</div>
</div>


<script src="../js/vue.js"></script>
<script src="../js/index.js"></script>
</body>
</html>

index.js:

/**
 * Created by Administrator on 2018/10/17 0017.
 */

var app = new Vue({

    el: "#app",
    data: {
        list: [
            {
                id: 1,
                name: 'iPhone7',
                price: 6188,
                count: 1
            },
            {
                id: 2,
                name: 'iPad Pro',
                price: 5888,
                count: 1
            },
            {
                id: 3,
                name: 'MacBook Pro',
                price: 21488,
                count: 1
            }
        ]
    },
    computed: {
        totalPrice:function () {
            var total = 0;
            for (var i=0;i<this.list.length;i++){
                var item = this.list[i];
                total += item.price*item.count;
            }
            return total.toString().replace(
                /\B(?=(\d{3})+$)/g,","
            );
        }
    },
    methods: {
        handleReduce:function (index) {
            if(this.list[index].count===1){
                return;
            }
            this.list[index].count --;
            
        },
        handleAdd:function (index) {
            this.list[index].count ++;

        },
        handdleRemove:function (index) {
            this.list.splice(index,1);
        }

    }
});

style.css:

[v-cloak]{
    display: none;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.10.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Vue实战之建立购物车
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档