이벤트 핸들러 - HTML 클릭 시 코드 실행하는 법
기존 js
<button type="button" onClick="동작">허위매물신고</button>
Vue
<button type="button" v-on:click="동작">허위매물신고</button>
<button type="button" @:click="동작">허위매물신고</button>
<template>
<div>
<h4>역삼동 원룸</h4>
<p>XX 만원</p>
<button type="button" v-on:click="report++">허위매물신고</button>
<p>신고 수: {{ report }}</p>
</div>
</template>
<script>
export default {
name: 'App',
data(){// 데이터 보관통
return {
report: 0, //신고수
}
}
}
</script>
this.데이터명 사용() 사용하지 않음<template>
<div>
<h4>역삼동 원룸</h4>
<p>XX 만원</p>
<!-- 함수에 () 달지 않음 -->
<button type="button" v-on:click="increase">허위매물신고</button>
<p>신고 수: {{ report }}</p>
</div>
</template>
<script>
export default {
name: 'App',
data(){// 데이터 보관통
return {
report: 0, //신고수
}
},
methods: { //methods 함수 보관함
increase(){
this.report += 1; //this 필수
},
},
}
</script>
<template>
<div class="menu">
<!-- -->
<a href="#" v-for="(item, idx) in menu" :key="idx">{{ item }}</a>
</div>
<div v-for="(item, idx) in products_obj" :key="idx">
<img :src="item.img">
<h4>{{ item.tit }}</h4>
<p>{{ item.price }} 만원</p>
<button type="button" v-on:click="item.report += 1">허위매물신고</button>
<p>신고 수: {{ item.report }}</p>
<hr>
</div>
</template>
<script>
export default {
name: 'App',
data(){// 데이터 보관통
return {
price1 : 60,
price2 : 70,
products: ['역삼동 원룸'],
products_obj: [
{
tit: '역삼동 원룸',
price: 60,
img: require('./assets/room0.jpg'),
report: 0,
},
{
tit: '천호동 원룸',
img: require('./assets/room1.jpg'),
price: 70,
report: 0,
},
{
tit: '마포구 원룸',
img: require('./assets/room2.jpg'),
price: 80,
report: 0,
},
],
menu: ['Home', 'Shop', 'About'],
blueColor: 'color: blue',
}
},
components: {
}
}
</script>
