export default {
props: {},
/*
부모 컴포넌트 → 자식 컴포넌트로 데이터를 전달 (props: ['mode', 'disabled'])
ex)
<child-component mode="edit"></child-component>
[자식] props: ['mode']
[사용]this.mode
*/
data() {},
/*
컴포넌트 내부 상태(state) 저장
data() {
return {
count: 0,
user: null
}
}
[사용]
this.count++
[특징]
컴포넌트의 로컬 상태
화면과 반응형 연결
값 변경 → 자동 렌더링
*/
components: {},
/*
다른 컴포넌트를 등록
[사용]
<PopupComponent />
[역할]
하위 컴포넌트 등록
재사용 UI 구성
*/
created(),
/*
컴포넌트 생성 직후 실행되는 라이프사이클
[사용]
created() {
console.log("컴포넌트 생성")
}
[특징]
DOM 생성 전
API 호출 많이 함
*/
mounted() {},
/*
DOM 렌더링 완료 후 실행
[사용]
mounted() {
console.log("DOM 생성 완료")
}
[특징]
DOM 접근 가능
차트 / 라이브러리 초기화
*/
beforeUnmount() {},
/*
컴포넌트 제거 직전에 실행
[사용]
beforeUnmount() {
console.log("컴포넌트 제거 전")
}
[특징]
이벤트 제거
타이머 제거
메모리 정리
*/
watch: {},
/*
특정 데이터 변화를 감시
[사용]
watch: {
count(newVal, oldVal) {
console.log("count 변경")
}
}
this.count = 10
[특징]
API 재요청
데이터 동기화
*/
computed: {},
/*
계산된 데이터
[사용]
computed: {
fullName() {
return this.firstName + " " + this.lastName
}
}
{{ fullName }}
[특징]
캐싱됨
data 기반 계산값
*/
methods: {}
/*
일반 함수
[사용]
methods: {
submit() {
console.log("전송")
}
}
<button @click="submit">
[특징]
이벤트 처리
로직 실행
*/
}
Vue 컴포넌트 구조 정리
export default {
props → 부모 데이터 받기
data → 내부 상태
components → 하위 컴포넌트 등록
lifecycle
created
mounted
beforeUnmount
computed → 계산된 데이터
watch → 데이터 변화 감시
methods → 함수
}
Vue 컴포넌트는 보통 이렇게 나눈다
입력
props
상태
data
계산
computed
이벤트
methods
감시
watch
생명주기
created
mounted
Vue의 options 가장 먼저 봐야 할 5가지
1️⃣ props 먼저 본다 (데이터의 시작점)
props: ['mode', 'disabled', 'carNo']
✔ 이유
- 이 컴포넌트가 어떤 데이터를 외부에서 받는지 알 수 있음
즉
부모 → 자식 데이터 흐름
예
mode = edit
disabled = true
carNo = "1234"
disabled = true
carNo = "1234"
이걸 보면 바로 알 수 있음
아 이 컴포넌트는
차량번호 선택 UI겠구나
차량번호 선택 UI겠구나
📌 props = 컴포넌트 입력값
2️⃣ data 구조 본다 (상태 확인)
data() {
return {
carList: [],
selectedCar: null,
loading: false
}
}
return {
carList: [],
selectedCar: null,
loading: false
}
}
✔ 이유
이 컴포넌트가 어떤 상태를 관리하는지 알 수 있음
예
carList
selectedCar
loading
selectedCar
loading
바로 추측 가능
차량 리스트 조회
차량 선택
로딩 상태 관리
차량 선택
로딩 상태 관리
📌 data = 컴포넌트 내부 상태
3️⃣ mounted / created 본다 (API 위치)
mounted() {
this.loadCarList()
}
this.loadCarList()
}
또는
created() {
this.fetchData()
}
this.fetchData()
}
✔ 이유
여기서 보통 API 호출이 시작됨
예
mounted → 화면 로드
API → 데이터 가져오기
API → 데이터 가져오기
📌 데이터가 어디서 오는지 확인
4️⃣ computed 본다 (화면용 데이터)
computed: {
filteredCars() {
return this.carList.filter(car => car.active)
}
}
filteredCars() {
return this.carList.filter(car => car.active)
}
}
✔ 이유
computed는 화면에 보여줄 데이터 가공
원본 데이터
↓
computed
↓
UI 출력
↓
computed
↓
UI 출력
예
전체 차량
↓
활성 차량만
↓
활성 차량만
📌 computed = 화면용 데이터
5️⃣ methods 본다 (실제 로직)
methods: {
loadCarList() {},
selectCar() {},
submit() {}
}
loadCarList() {},
selectCar() {},
submit() {}
}
✔ 이유
여기에 핵심 비즈니스 로직이 있음
예
API 호출
데이터 변경
이벤트 처리
데이터 변경
이벤트 처리
📌 methods = 실제 기능
⭐ 실무에서 Vue 코드 읽는 순서
개발자들은 보통 이렇게 본다
1 props
2 data
3 mounted / created
4 computed
5 methods
6 watch
2 data
3 mounted / created
4 computed
5 methods
6 watch