7.[Vue入门]计数器案例

定义方法

// 减法
"sub": function () {
    console.log('sub')
    if (this.judgmentNum(this.num)) {
        this.num--;
    }
},
    
// 加法
"add": function () {
    console.log('add')
    if (this.judgmentNum(this.num)) {
        this.num++;
    }
},

// 重置
"reset": function () {
    console.log('reset')
    this.num = 5
},

// 判断数字边界
"judgmentNum": function (num) {
    console.log('num ==' + num)
    if (num <= 0) {
        return alert("别点了最小值了")
    }
    if (num >= 10) {
        return alert("别点了最大值了")
    }
    return true
}

完整代码

<!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>
</head>
<body>
    <div id="app">
        <button @click="sub">-</button>
        <span v-text="num"></span>
        <button @click="add">+</button>
        <button @click="reset">重置</button>
    </div>

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

    <script>
        var app = new Vue({
            el: '#app',
            data: {
                num: 5
            },
            methods: {
                "sub": function () {
                    console.log('sub')
                    if (this.judgmentNum(this.num)) {
                        this.num--;
                    }
                },
                "add": function () {
                    console.log('add')
                    if (this.judgmentNum(this.num)) {
                        this.num++;
                    }
                },
                "reset": function () {
                    console.log('reset')
                    this.num = 5
                },
                "judgmentNum": function (num) {
                    console.log('num ==' + num)
                    if (num <= 0) {
                        return alert("别点了最小值了")
                    }
                    if (num >= 10) {
                        return alert("别点了最大值了")
                    }
                    return true
                }
            }
        })
    </script>
</body>
</html>

7.[Vue入门]计数器案例
https://元气码农少女酱.我爱你/4136a686de1d/
作者
元气码农少女酱
发布于
2023年5月28日
许可协议