<div id="app">
<button v-on:click="counter += 1">클릭</button>
<p>위 버튼을 클릭한 횟수는 {{counter}} 번 입니다.</p>
<button v-on:click="increase">클릭</button>
<p>위 버튼을 클릭한 횟수는 {{counter}} 번 입니다.</p>
</div>
<script>
new Vue({
el: '#app',
data: {
counter: 0,
},
methods:{
increase:function(event){
this.counter++;
}
}
});
</script>
event.preventDefault();
처럼 method내에서 작업을 할 수도 있지만 method는 DOM의 이벤트를 처리하는 것 보다 data 처리를 위한 로직만 작업하는 것이 좋다.<!-- 클릭 이벤트 전파가 중단된다. -->
<a v-on:click.stop="doThis"></a>
<!-- 제출 이벤트가 페이지를 다시 로드하지 않는다. -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- Key가 Enter일때만 submit 동작 -->
<input v-on:keyup.enter="submit">
Key Code
.enter (.13)
.tab
.delete ("Delete"와 "Backspace" 키 모두 캡쳐)
.esc
.space
.up
.down
.left
.right
<input type="text" v-model="id" ref="id">
...
<script>
console.log(this.$refs.id.value);
<script>
<style>
.active {
background: rgb(100, 140, 230);
color: white;
}
</style>
<div id="app">
<div v-bind:class="{active: isActive}">
Vue CSS 적용
</div>
</div>
<script>
new Vue({
el: "#app",
data : {
isActive: false,
},
});
</script>