728x90
반응형
화면에서 입력받은 값으로 Dynamic Select Box를 생성하는 기초 예제
*아래 예제는 전 예제에 이어서 진행(아래 링크 참고)
*자세한 설명 생략
▷ 기초 예제 - Dynamic Select Box 생성
*Event Handling 관련 아래 링크 참고
*프로젝트 구조 및 CSS 생략
*파일 경로 필요시 App.vue 경로 참고
- CompositionTestCmp.vue
<template>
<hr>
<h2>[ Composition Example ]</h2>
<button @click="initTest();">초기화</button>
<div>
<h4> 입력 :
<input v-model="inData" @keyup.enter="addList(inData);" placeholder="아무거나 입력해주세요.">
</h4>
</div>
<div>
<h4> 선택 :
<select v-model="select">
<option value="-" selected>선택해주세요.</option>
<option v-for="(item, i) in list" :key="i" :value="item">{{ item }}</option>
</select>
</h4>
</div>
<div>
<h4> 확인 :
{{ select }}
</h4>
</div>
<div>
<h4> 리스트 현황</h4>
<div v-for="(item, i) in list" :key="item">
{{ i }} → {{ item }}
</div>
</div>
<hr>
</template>
<script setup>
import { ref } from 'vue';
/** 반응형 객체 */
let inData = ref("");
let list = ref([]);
let select = ref("-");
/** 리스트 값 추가 */
function addList(data) {
if (data == "" || data == null) {
alert("입력 값이 없습니다.");
} else {
list.value.push(data);
inData.value = "";
}
}
/** 초기화 */
function initTest() {
inData.value = "";
list.value = [];
select = "-";
}
</script>
- App.vue
<template>
<div id="HeaderCmp"><HeaderCmp /></div>
<div id="TestDiv">
<p id="CompositionTestCmp"><CompositionTestCmp /></p>
<p id="IfTestCmp"><IfTestCmp /></p>
<p id="ForTestCmp"><ForTestCmp /></p>
</div>
</template>
<script>
import HeaderCmp from '@/components/HeaderCmp.vue'
import ForTestCmp from '@/components/Test/ForTestCmp.vue'
import IfTestCmp from '@/components/Test/IfTestCmp.vue'
import CompositionTestCmp from '@/components/Test/CompositionTestCmp.vue'
export default {
components: {
HeaderCmp,
ForTestCmp,
IfTestCmp,
CompositionTestCmp
}
}
</script>
▷ 결과 확인
*http://localhost:8080
▷ 참고
Event Handling | Vue.js
VueConf Toronto - Join the premier Vue.js conference | 9-10 Nov 2023 - Toronto, CanadaView Schedule Use code VUEJS to get 15% off
vuejs.org
▷ 관련 글
Vue3 기초 예제 프로젝트 정리
Vue3 기초 예제 프로젝트 구조 및 소스 중간 정리 *관련 글이 점점 늘어나 하나로 통합하기 위함 *이 글에서 만 싱크 맞춤 *주석 이외 설명 생략 *자세한 설명 생략 ▷ 프로젝트 전체 구조 < 파일 따
coding-today.tistory.com
728x90
728x90
'▶ Front-End > Vue.js' 카테고리의 다른 글
Vue3 기초 예제 - HTTP 통신(Axios) (2) | 2023.10.27 |
---|---|
Vue3 기초 예제 - Router(메뉴 네비게이션) (0) | 2023.10.26 |
Vue 기초 예제 - Directive(조건/반복) (0) | 2023.10.23 |
Vue3 Project 생성 및 실행 (0) | 2023.10.20 |
Vue 설치 방법 (2) | 2023.10.20 |
댓글