Vue练习(一)

Vue练习(一)

  • v-model:用于在表单类元素上双向绑定数据,例如 在输入框上使用时,输入的内容会实时映射到绑定的数据上。
  • v-for:
  1. 将一个数组遍历或枚举一个对象循环,它的表达 式需结合 in 来使用,类似 item in items 的形式。
  2. v-for 的表达式支持一个可选参数作为当前项的索引
  3. 除了数组外, 对象的属性也是可以遍历
  4. v-for 还可以迭代整数

练习内容:用Vue完成一个简易的购物车

生成结果:
在这里插入图片描述
代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="vue .js"></script>
</head>
<style>
    td {
        text-align: center;
    }

    table,
    th,
    td {
        padding: 8px 16px;
        border: 1px solid black;
    }

    table {
        empty-cells: show;
        border-collapse: collapse;
    }

    th {
        height: 30px;
        background-color: darkgray;
    }

    button {
        background-color: white;
        border: 1px solid black;
    }
</style>

<body>
    <div id="app">
        商品名称:<input type="text" v-model="name">
        商品数量:<input type="text" v-model="num">
        商品价格:<input type="text" v-model="price">
        <button @click="newAdd()">添加</button>
        <table>
            <tr>
                <th></th>
                <th>商品名称</th>
                <th>商品价格</th>
                <th>购买数量</th>
                <th>是否移除</th>
            </tr>
            <tr v-for="(i,index) in shop">
                <td>{{index+1}}</td>
                <td>{{i.name}}</td>
                <td>{{i.price}}</td>
                <td>
                    <button @click="Click1(index)">-</button>
                    {{i.num}}
                    <button @click="Click2(index)">+</button>
                </td>
                <td>
                    <button @click="Remove(index)">移除</button>
                </td>
            </tr>
        </table>
        <div>总计:{{sum}}</div>
    </div>
</body>
<script>
    var app = new Vue({
        el: "#app",
        data: {
            shop: [{
                    name: "小猪",
                    price: 50,
                    num: 1,
                    show: true
                },
                {
                    name: "小狗",
                    price: 50,
                    num: 1,
                    show: true
                },
                {
                    name: "小猫",
                    price: 50,
                    num: 1,
                    show: true
                }
            ],
            name: "",
            price: "",
            num: ""
        },
        methods: {
            Click1: function (index) {
                this.shop[index].num = this.shop[index].num - 1;
                if (this.shop[index].num <= 0) {
                    this.shop[index].num = 0;
                }
            },
            Click2: function (index) {
                this.shop[index].num = this.shop[index].num + 1;
            },
            Remove: function (index) {
                this.shop.splice(index, 1);
            },
            newAdd: function () {

                this.shop.push({
                    name: this.name,
                    price: parseInt(this.price),
                    num: parseInt(this.num),
                    show: true
                })
            }
        },
        computed: {
            sum: function () {
                var s = 0;
                //console.log(this.shop[0].show)
                for (var i = 0; i < this.shop.length; i++) {
                    if (this.shop[i].show) {
                        s = s + (this.shop[i].price) * (this.shop[i].num);
                    }
                }
                return s;
            }
        }
    })
</script>

</html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值