v-on:[이벤트이름]="표현식"
@[이벤트이름]="표현식"
@click="test($event)" <body>
<div id="app">
금액: <input type="text" v-model.number="amount" /><br />
<button v-on:click="balance += parseInt(amount)">입금</button>
<button @click="balance -= parseInt(amount)">인출</button> <br />
<h3>계좌 잔고: {{ balance }}</h3>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
let vm = Vue.createApp({
name: 'App',
data() {
return {
amount: 0,
balance: 0,
};
},
}).mount('#app');
</script>
</body>
<body>
<div id="app">
금액: <input type="text" v-model.number="amount" /><br />
<button v-on:click="deposit">입금</button>
<button @click="withdraw">인출</button> <br />
<h3>계좌 잔고: {{ balance }}</h3>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
let vm = Vue.createApp({
name: 'App',
data() {
return {
amount: 0,
balance: 0,
};
},
methods: {
deposit() {
let amt = parseInt(this.amount);
if (amt <= 0) {
alert('0보다 큰 값을 예금하세요');
} else {
this.balance += amt;
this.amount = 0;
}
},
withdraw() {
let amt = parseInt(this.amount);
if (amt <= 0) {
alert('0보다 큰 값을 인출하세요');
} else if (amt > this.balance) {
alert('잔고보다 많은 금액을 인수할 수 없습니다');
} else {
this.balance -= amt;
}
this.amount = 0;
},
},
}).mount('#app');
</script>
</body>
주요 속성
target
key
charCode
button
···
주요 메서드
preventDafault() : 기본 이벤트 자동 실행 금지
···
<a> : 클릭했을 때 href 특성의 경로로 페이지 이동preventDefault() <body>
<div id="app">
<!-- 마우스오른쪽 눌러도 메뉴를 띄우지 X -->
<div @contextmenu="ctxStop" style="position: absolute">
<a href="https://facebook.com" @click="confirmFB">페이스북</a>
</div>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
let vm = Vue.createApp({
name: 'App',
data() {
return {};
},
methods: {
ctxStop(e) {
e.preventDefault();
},
confirmFB(e) {
// comfirm(): 확인(true) / 취소(false)
if (!confirm('페이스북으로 이동?')) {
e.preventDefault();
}
},
},
}).mount('#app');
</script>
</body>
@이벤트.prevent = preventDefault() <div id="app">
<!-- 마우스오른쪽 눌러도 메뉴를 띄우지 X -->
<div @contextmenu.prevent style="position: absolute">
<a href="https://facebook.com" @click="confirmFB">페이스북</a>
</div>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
let vm = Vue.createApp({
name: 'App',
data() {
return {};
},
methods: {
confirmFB(e) {
// comfirm(): 확인(true) / 취소(false)
if (!confirm('페이스북으로 이동?')) {
e.preventDefault();
}
},
},
}).mount('#app');
</script>

@이벤트.enter="..." : 엔터 쳤을 때 이벤트 핸들러 호출@keyup.ctrl.enter="..." : ctrl+enter 조합 시 이벤트 핸들러 호출@keyup.ctrl.c="..." : ctrl+c 조합<button @click.exact="num=num+1"
@click.ctrl.exact="num=num+10"
@click.ctrl.alt.exact="num=num+100">