Vue
2021-11-29 (Vue.js - 템플릿 문법(4))
BSYeop
2021. 11. 29. 17:58
Method & Event를 사용해보자
<template>
<div>
<!-- 증가, 감소 -->
<h1>{{count}}</h1>
<button v-on:click="addNumber(1)">증가</button>
<button v-on:click="minusNumber">감소</button>
<button v-on:mouseover="addNumber(10)">마우스 오버 10 증가</button>
<hr>
<!-- 문자 출력 -->
<h2>{{message}}</h2>
<button @click="greeting">인사하기</button>
<button @click="sayGoodbye">작별하기</button>
<hr>
<!-- Event 사용 -->
<h2>{{message2}}</h2>
<div @click="getMousePosition" class="box">
</div>
</div>
</template>
<script>
export default {
name: 'App',
data(){
return {
count: 0,
message: "",
message2: ""
};
},
methods: {
getMousePosition(event){
console.log(event);
this.message2 = `마우스의 위치는 ${event.pageX},${event.pageY}입니다`;
},
greeting(){
this.message = "안녕하세요";
},
sayGoodbye(){
this.message = "안녕히가세요";
},
addNumber(value){
console.log("add Number")
this.count = this.count + value;
},
minusNumber: ()=>{
console.log(this);
this.count = this.count - 1;
}
}
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
button {
width: 100px;
height: 100px;
font-size: 24px;
}
.box {
width: 300px;
height: 300px;
background: olive;
}
</style>
1. 증가(클릭 시 +1), 감소(클릭 시 -1), 10증가(마우스 오버 시 +10)
2. 인사하기(클릭 시 안녕하세요), 작별하기 (클릭 시 안녕히가세요)
3. 박스 클릭 시 마우스의 x,y 좌표 출력