Vue案例之計數器

在這裏插入圖片描述
在這裏插入圖片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div id="app">
        <h2>當前計數:{{counter}}</h2>
        <!--
        <button v-on:click="counter++">+</button>
        <button v-on:click="counter--">-</button>
        -->
   	    <button v-on:click="add">+</button>
        <!--v-on還可以這樣用@表示    -->
        <button @click="sub">-</button>
</div>

<script src="../js/vue.js"></script>
<script>

    const obj = {
        counter: 0,
    }
    const app = new Vue({
        el: '#app',
        data: obj,
        methods: {
            add: function () {
                console.log('add被執行');
                this.counter++
            },
            sub: function () {
                console.log('sub被執行');
                this.counter--
            }
        },
        beforeCreate: function () {

        },
        created: function () {
            console.log('created');
        },
        mounted: function () {
            console.log('mounted');
        }
    })

    //1.拿到button元素

    //2.添加監聽事件
</script>
</body>
</html>

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章