할머니의 콤퓨타 도전기

[Vue.js] 이벤트 (Events) 본문

Web Front-end/Vue.js

[Vue.js] 이벤트 (Events)

ji.o.n.e 2020. 10. 27. 23:41
 

Event Handling — Vue.js

Vue.js - The Progressive JavaScript Framework

vuejs.org

  • onClick 이벤트
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue Study</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        {{age}} <br>
        <!-- event -->
        <button v-on:click="plus">plus</button>
        <button v-on:click="minus">minus</button>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                age:23,
            },
            methods: {
                plus(){
                    ++this.age;
                },
                minus(){
                    --this.age;
                }
            },
        })
    </script>
</body>

</html>

 

  • 실행 화면

plus 버튼을 클릭하면 age가 1씩 증가하고, minus 버튼을 클릭하면 1씩 감소한다.

  • onSubmit 이벤트
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue Study</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <!-- submit.prevent : page reload 안함 -->
        <form v-on:submit.prevent="submit">
            <input type="text"><br>
            <button type="submit">submit</button>
        </form>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
            },
            methods: {
                submit() {
                    alert('submitted');
                    console.log('hello');
                }
            },
        })
    </script>
</body>

</html>

 

  • 실행 화면

Comments