Vue系列教程-05-栗子购物车添加与删除

vue 动态数据和点击事件的小案例

我是分割线


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>vue指令栗子</title>
</head>
<body>

    <div class="input-num" id="app">
        <button @click="sub">-</button>
        <span>{{ num }}</span>
        <button @click="add">+</button>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>

        var app = new Vue({
            el: '#app',
            data: {
                num: 0,
            },
            methods: {
                sub: function (){
                    if (this.num > 0){
                        this.num --;
                    }else {
                        alert("最小为0")
                    }
                },
                add: function (){
                    if (this.num < 10){
                        this.num ++;
                    }else {
                        alert("最大为10")
                    }
                },
            }
        });
    </script>
</body>
</html>

我是分割线

前台效果:




    vue指令栗子
{{ num }}
点赞

发表评论