- 여러개의 root Element 선언이 가능
<template>
<vue2 msg="vue2" />
<vue3 msg="vue3" />
</template>
- compositon API의 핵심
- setup method에 data, computed, method, lifecycle hook등을 설정할 수 있음
- setup메소드 내에서 reactive 선언
data() {
return {
count: 0
};
},
computed: {
absCount() {
return Math.abs(this.count);
},
},
- setup() 내에 정의
- reactive 활용
import { computed, reactive } from 'vue';
...
setup() {
const state = reactive({
count: 0,
absCount: computed(() => Math.abs(state.count)),
});
...
methods: {
onIncrement() {
this.count += 1;
},
onDecrement() {
this.count -= 1;
},
},
- setup메소드에 method를 정의
const onIncrement = () => {
state.count += 1;
};
const onDecrement = () => {
state.count -= 1;
};
setup() {
return {
onIncrement,
onDecrement,
}
}
mounted() {
this.count = 3;
this.$refs.btnInc.textContent = '+1';
this.$refs.btnDec.textContent = '-1';
}
- lifecycle hook을 setup메소드에 정의
setup() {
const btnInc = ref();
const btnDec = ref();
onMounted(() => {
state.count = 3;
btnInc.value.textContent = '+1';
btnDec.value.textContent = '-1';
});
}
props: {
msg: String,
},
methods: {
onMsg() {
console.log(this.msg);
}
}
props: {
msg: String,
},
setup(props) {
return {
onMsg() {
console.log(props.msg);
}
}
}
this.$emit('submit', result);
setup(props, { emit }) {
emit('submit', result);
}
yarn
yarn run serve