<template>
<div>
<h1>{{first.name}}</h1>
<h1>{{second}}</h1>
<div>
</template>
<script>
import {reactive, ref } from @vue/composition-api
const useValues = () => {
const reactiveValue = reactive({ name: 'tom' })
const refValue = ref(10)
return {reactiveValue, refValue }
}
onMounted(), onUnmounted(), onUpdated()를 사용하여 Component생명 주기에 프로그래밍 방식으로 연결 가능
import { onMounted, onUpdated, onUnmounted } from '@vue/composition-api'
export default {
setup () {
onMounted(() => {
//mounted
})
onUpdated(() => {
//updated
})
onUnmounted(() => {
//destroyed
})
}
}
provide(), inject()를 사용하여 반응형API를 사용하는동안 Vue 의존성 주입 시스템 활용 가능
<script setup>
import { ref, onMounted } from 'vue'
// 반응형 상태
const count = ref(0)
// 상태를 변경하고 업데이트를 트리거하는 함수
function increment() {
count.value++
}
// 생명주기 훅
onMounted(() => {
console.log(`숫자를 세기 위한 초기값은 ${count.value} 입니다.`)
})
</script>
<template>
<button @click="increment">숫자 세기: {{ count }}</button>
</template>
<template>
<div class="home">
<p>{{ person1.name }} {{ person1.value }}</p>
<button @click="handleClick">click</button>
</div>
</template>
<script>
import { ref, reactive } from "vue";
export default {
name: "HOME",
setup() {
// 데이터를 ref, reactive로 감싸면 반응형으로 바뀝니다.
const person1 = ref({ name: "nkh", age: 29 });
const person2 = reactive({ name: "nki", age: 26 });
const handleClick = () => {
// ref로 감싼 값을 변경할 때는 value로 한번 들어가주고 값을 바꿉니다.
person1.value.age = 30;
// reactive는 바로 값을 바꿉니다.
person2.age = 30;
};
// ref값은 return을 거치게되면 person1.value.age는 person1.age로 바뀝니다. (template에서는 person1.age로 사용합니다)
return { person1, handleClick };
}
};
</script>

<template>
<div>
<button @click="increment">Increment</button>
<p>Count: 8</p>
</div>
</template>
<script setup> // script setup 적용
import { ref } from 'vue';
const count = ref(0);
const increment = () => {
count.value++;
};
</script>
<template>
<dlv class="home">
<!-- child 컴포넌트에게 props 내림 -->
<PostList :posts="posts" />
</div>
</template>
<script>
// 사용할 컴포넌트 import
import PostList from '../components/PostList.vue'
import { ref } from 'vue';
export default {
name: 'Home',
// 사용할 컴포넌트를 넣어줍니다.
components: { PostList },
setup() {
const posts = ref([
{ title: '1번 타이틀', body: '1번 제목', id: 1 },
{ title: '2번 타이틀', body: '2번 제목', id: 2 },
]);
return { posts }
}
}
</script>
<template>
<div>
{{ post.title }}
{{ post.body }}
</div>
</template>
<script>
export default {
// 사용할 props를 배열내에 정의합니다.
props: ["posts"],
setup(props) {
console.log(props.posts); // 받은 prop 사용가능
}
};
</script
Pinia가 애플리케이션 전역 상태를 관리하여 Component간 데이터 전달을 단순화하거나 props전달의 필요성을 줄일 수 있다.
그러나 모든 데이터를 Pinia 스토어에 저장하면 편리할 수 있지만 애플리케이션 성능에 부담을 줄 수 있다.
(특히, 간단한 부모 -> 자식 컴포넌트 간 데이터 전달은 props 사용이 더 경량화되고 효율적이다)