본문 바로가기
FrontEnd/Vue 2

[Vue.js] v-on 디렉티브를 통한 event Handle Methods binding

by Chaedie 2022. 9. 18.
728x90

v-on 디렉티브

  • v-on:keyup 과 같은 v-on 디렉티브를 통해 eventListener를 쉽게 붙일 수 있다.
<!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>Document</title>
  </head>
  <body>
    <div id="app">
      <button v-on:click="logText">Click me</button>
      <input type="text" v-on:keyup.enter="logText" />
      <button>add</button>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
      new Vue({
        el: '#app',
        data: {},
        methods: {
          logText: function () {
            console.log('clicked');
          },
        },
      });
    </script>
  </body>
</html>

댓글