단순 코드 기록/Vue

Vue_v-on

일일일코_장민기 2024. 3. 7. 12:11
728x90
v-on:이벤트명="처리 함수"
@이벤트명="처리 함수"
<template>
    <div>
        <!-- v-on:이벤트명="처리 함수" -->
        <button v-on:click="handleEvent">v-on:click="handleEvent"</button><br>
        <button v-on:click="xxx('홍길동')">v-on:click="xxx('홍길동')"</button><br>
        <button v-on:click="yyy('홍길동', $event)">v-on:click="yyy('홍길동', $event)"</button>

        <hr>

        <!-- @이벤트명="처리 함수" -->
        <button @click="handleEvent">@click="handleEvent"</button><br>
        <button @click="xxx('홍길동')">@click="xxx('홍길동')"</button><br>
        <button @click="yyy('홍길동', $event)">@click="yyy('홍길동', $event)"</button>

        <hr>
        <!-- 속성을 사용한 데이터 전송 -->
        <!-- 함수에서는 data-를 제외한 부분을 사용하며, dataset에 저장됨 -->
        <button v-on:click="handleEvent2"
            data-my="aaa"
            data-my2='{"name":"홍길동", "age":"20"}'>속성을 이용한 데이터 전송</button><br>

       
    </div>
  </template>
 
  <script>
  export default {
    data:function(){
        return {
            num: 100
        }
    },
    methods: {
        handleEvent :function(data){
            console.log("ok" + this.num, data)
        },
        xxx :function(name){
            console.log("ok" + this.num, name)
        },
        yyy :function(name, data){
            console.log("ok" + this.num, name, data)
        },
        handleEvent2 :function(e){
            console.log("ok", e, e.target)          //e:              PointerEvent
                                                    //e.target:       태그 자체가 출력
            console.log(this)                       //VueComponent
            console.log(e.target.dataset)           //DOMStringMap
            console.log(e.target.dataset.my)        //aaa
            console.log(e.target.dataset.my2)       //실질적으로 문자열: {"name":"홍길동", "age":"20"}
            let parsedJSON = JSON.parse(e.target.dataset.my2)   //JSON 파싱
            console.log(parsedJSON.name, parsedJSON.age)        //파싱 후 출력
           
           
        },

       
       
    }
  }
  </script>
 
  <style>
 
  </style>