티스토리 뷰
클릭 이벤트
<!-- v-on:의 축약: @ -->
<button v-on:click="더하기">버튼</button
<button @click="더하기">버튼</button>
ex) 버튼 누르면 숫자 증가시키기
<template>
<button @click="increase++">1증가버튼</button> // click하면
<span>{{ increase }}</span> // 초기값 0이 +1
</template>
<script>
export default {
name: 'App',
data() {
return {
increase: 0,
};
},
components: {},
};
</script>
코드가 길 땐 함수로 작성하기
<template>
<button @click="increase()">1증가버튼</button> <!-- 클릭 시 함수() 실행 -->
<span>{{ increase }}</span>
</template>
<script>
export default {
name: 'App',
data() {
return {
increase: 0,
};
},
methods: {
increase() {
this.report++; // 데이터 사용 시 this.붙여주기
},
},
components: {},
};
</script>
반복문과 함께 사용하기
<template>
<div v-for="(a, i) in products" :key="i">
<button @click="increase[i]++">1증가버튼</button> // click하면
<span>{{ increase[i] }}</span> // 초기값 0이 +1
</div>
</template>
<script>
export default {
name: 'App',
data() {
return {
report: [0, 0, 0], // 0번째버튼값, 1번째버튼값, 2번째버튼값
};
},
components: {},
};
</script>
'Library | Framework > Vue JS' 카테고리의 다른 글
| [Vue.js] 7. component (0) | 2023.08.04 |
|---|---|
| [Vue.js] 6. import / export (0) | 2023.08.04 |
| [Vue.js] 5. 이미지 & 동적 UI (feat.모달창) (0) | 2023.08.03 |
| [Vue.js] 3. 반복문 v-for, 조건문 v-if (0) | 2023.08.03 |
| [Vue.js] 2. 데이터바인딩 (0) | 2023.08.03 |
댓글
