{{ }}
์ ๊ฐ๋จํ ์ฌ์ฉํ๋ฉด ๋งค์ฐ ํธ๋ฆฌํ๋ค. ํ์ง๋ง ํ
ํ๋ฆฟ ํํ์ ๋ด ์ฝ๋๊ฐ ๊ธธ์ด์ง๋ฉด ๊ฐ๋
์ฑ์ด ๋งค์ฐ ๋จ์ด์ง๋ค. ์๋ฅผ๋ค์ด ์๋์ ๊ฐ์ ๋<template>
<div>
<h2>{{ teacher.name }}</h2>
<h3>๊ฐ์๊ฐ ์์ต๋๊น?</h3>
<p>{{ teacher.lectures.length > 0 ? "์๋ค." : "์๋ค." }}</p>
</div>
</template>
import { reactive } from "vue";
export default {
setup() {
const teacher = reactive({
name: "์ฑ์",
lectures: ["HTML/CSS", "JavaScirpt", "Vue3"],
});
return {
teacher,
};
},
};
์ด๋ ๊ฒ ๊ฐ์๊ฐ ์์ ๋ ํ
ํ๋ฆฟ ๋ฌธ๋ฒ์ ๊ธธ์ด์ง๊ฒ ๋๋ค. ์ด๋ฅผ ์ฌ๋ฌ๊ณณ์์ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉํด์ผํ ๋ ๋นํจ์จ์ ์ด๋ค. ์ด๋ด๋ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ณ์ฐ๋ ์์ฑ(computed property)
์ด๋ค. computed
๋ฅผ ํ์ฉํด๋ณด์.
<template>
<div>
<h2>{{ teacher.name }}</h2>
<h3>๊ฐ์๊ฐ ์์ต๋๊น?</h3>
<!-- <p>{{ teacher.lectures.length > 0 ? "์๋ค." : "์๋ค." }}</p> -->
<p>{{ hasLecture }}</p>
</div>
</template>
import { computed, reactive } from "vue";
export default {
setup() {
const teacher = reactive({
name: "์ฑ์",
lectures: ["HTML/CSS", "JavaScirpt", "Vue3"],
});
// arrow function์ ๋ช
๋ น์ด๊ฐ ํ๋์ผ ๋๋ return๊ณผ ์ค๊ดํธ๋ฅผ ๋บผ ์ ์๋ค.
//const hasLecture = computed(() => {
// return teacher.lectures.length > 0 ? "์๋ค." : "์๋ค.";
//});
const hasLecture = computed(() => teacher.lectures.length > 0 ? "์๋ค." : "์๋ค."; );
return {
teacher,
hasLecture,
};
},
};
์ด๋ ๊ฒ hasLecture
๋ผ๋ computed
๋ฅผ ์ ์ํด ๋ง๋ค์๋ค. ๊ทธ ์์ callbackํจ์๋ฅผ ๋ฃ์ด ๋ง๋ค์๋ค. ๊น๋ํ ๊ฒฐ๊ณผ๋ฅผ ๋๊ฐ์ด ์ถ๋ ฅ๋๋ค.
<template>
<div>
<h2>{{ teacher.name }}</h2>
<h3>๊ฐ์๊ฐ ์์ต๋๊น?</h3>
<!-- <p>{{ teacher.lectures.length > 0 ? "์๋ค." : "์๋ค." }}</p> -->
<p>{{ hasLecture }}</p>
<!-- ๋ฉ์๋๊ธฐ๋๋ฌธ์ ๋์ ๊ดํธ๋ฅผ ๋ฃ์ด์ค์ผ ํ๋ค. -->
<p>{{ existLecture() }}</p>
</div>
</template>
import { computed, reactive } from "vue";
export default {
setup() {
const teacher = reactive({
name: "์ฑ์",
lectures: ["HTML/CSS", "JavaScirpt", "Vue3"],
});
//arrow function์ ๋ช
๋ น์ด๊ฐ ํ๋์ผ ๋ return๊ณผ ์ค๊ดํธ๋ฅผ ๋บผ ์ ์๋ค.
// const hasLecture = computed(() => {
// return teacher.lectures.length > 0 ? "์๋ค." : "์๋ค.";
// });
const hasLecture = computed(() =>
teacher.lectures.length > 0 ? "์๋ค." : "์๋ค.",
);
const existLecture = () =>
teacher.lectures.length > 0 ? "์๋ค." : "์๋ค.";
return {
teacher,
hasLecture,
existLecture,
};
},
};
์ถ๋ ฅํ๋ฉด ์๋์ ๊ฐ์ ๊ฐ์ด ๋์จ๋ค. ๊ฐ์ ๊ฐ์ด ๋์จ๋ค.
computed
๊ฐ ์ฑ๋ฅ๋ฉด์์ ๋ ์ ๊ฒ ๋ ๋ค. ์ด์ ๋ 'hasLecture'์์์ ๊ณ์ฐ๋ ๊ฐ์ด ์บ์๋๊ธฐ ๋๋ฌธ์ด๋ค. ํ๋ง๋๋ก ํ๋ฒ๋ง ๋ถ๋ฌ์ ๊ณ์ํด์ ์ฌ์ฉํ๋ค๋ ๊ฒ์ด๋ค. ๋ฉ์๋์ ๊ฒฝ์ฐ์๋ ๊ณ์ํด์ ๋ถ๋ฌ์ ์ฌ์ฉํ๋ค. console
์ ์ฐ์ด์ ํ์ธํด๋ณด์.import { computed, reactive } from "vue";
export default {
setup() {
const teacher = reactive({
name: "์ฑ์",
lectures: ["HTML/CSS", "JavaScirpt", "Vue3"],
});
//arrow function์ ๋ช
๋ น์ด๊ฐ ํ๋์ผ ๋ return๊ณผ ์ค๊ดํธ๋ฅผ ๋บผ ์ ์๋ค.
// const hasLecture = computed(() => {
// return teacher.lectures.length > 0 ? "์๋ค." : "์๋ค.";
// });
const hasLecture = computed(() => {
console.log("computed");
return teacher.lectures.length > 0 ? "์๋ค." : "์๋ค.";
});
const existLecture = () => {
console.log("method");
teacher.lectures.length > 0 ? "์๋ค." : "์๋ค.";
};
return {
teacher,
hasLecture,
existLecture,
};
},
};
์ด๋ ๊ฒ console
์ ์ฐ์์ ๋๋ ์ถ๋ ฅ๋๋๊ฒ ๊ฐ 1๊ฐ์ง๋ง. ํ
ํ๋ฆฟ์์ ์ฌ๋ฌ๋ฒ ์ฌ์ฉํ๊ฒ๋๋ฉด ์ด๋ป๊ฒ ๋ ๊น?
<template>
<div>
<h2>{{ teacher.name }}</h2>
<h3>๊ฐ์๊ฐ ์์ต๋๊น?</h3>
<!-- <p>{{ teacher.lectures.length > 0 ? "์๋ค." : "์๋ค." }}</p> -->
<p>{{ hasLecture }}</p>
<p>{{ hasLecture }}</p>
<p>{{ hasLecture }}</p>
<p>{{ hasLecture }}</p>
<!-- ๋ฉ์๋๊ธฐ๋๋ฌธ์ ๋์ ๊ดํธ๋ฅผ ๋ฃ์ด์ค์ผ ํ๋ค. -->
<p>{{ existLecture() }}</p>
<p>{{ existLecture() }}</p>
<p>{{ existLecture() }}</p>
<p>{{ existLecture() }}</p>
</div>
</template>
์ด๋ ๊ฒ ์ฌ๋ฌ๋ฒ ์ถ๋ ฅ๋๋ค. ์ด์ ๋ computed
๋ ๊ณ์ฐ๋ ๊ฒฐ๊ณผ๋ฅผ ์บ์ํ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ณด๊ดํ์๋ค๊ฐ ์์ฒญํ์ ๋ ๊ทธ ์บ์๋ ๋ฐ์ดํฐ๋ฅผ ๋๋ ค์ค๋ค. ํ์ง๋ง method
๋ ์คํ๋ ๋ ๋ง๋ค ์ฐํ๋ค. ์ด๋ฌํ ์บ์๊ฐ ๋ค์ ๊ณ์ฐ๋๋ ์์ ์ ๋ฐ์ํ ๋ฐ์ดํฐ(hasLecture
)๊ฐ ๋ณ๊ฒฝ๋๋ ์์ ์ด๋ค.
<template>
<div>
<h2>{{ fullName }}</h2>
</div>
</template>
import { computed, ref } from "vue";
export default {
setup() {
const firstName = ref("ํ");
const lastName = ref("๊ธธ๋");
const fullName = computed(() => firstName.value + " " + lastName.value);
console.log("console ์ถ๋ ฅ:", fullName.value);
return {
fullName,
};
},
};
์ด๋ ๊ฒ ์ถ๋ ฅ์ด ๋๋ค. ์ฌ๊ธฐ์ ๊ฐ์ ๋ณ๊ฒฝํด๋ณด์.
import { computed, ref } from "vue";
export default {
setup() {
const firstName = ref("ํ");
const lastName = ref("๊ธธ๋");
const fullName = computed(() => firstName.value + " " + lastName.value);
console.log("console ์ถ๋ ฅ:", fullName.value);
//๋ณ๊ฒฝ๋๋ ๊ฐ
fullName.value = "์ด ์ฌ์ฐ";
return {
fullName,
};
},
};
์ด๋ ๊ฒ readonly
๋ผ๊ณ ๊ฒฝ๊ณ ํ์๊ฐ ๋ฌ๋ค. ๋ง์ฝ ์ฐ๊ธฐ๊ฐ ํ์ํ๋ค๋ฉด getter
์ setter
ํจ์๋ฅผ ์ ์ฉํ ์ ์๋ค. ์ฝ๋ฐฑํจ์๊ฐ ์๋ ๊ฐ์ฒด๋ฅผ ์ ์ฉํด์..
import { computed, ref } from "vue";
export default {
setup() {
const firstName = ref("ํ");
const lastName = ref("๊ธธ๋");
const fullName = computed({
get() {
//์ฃผ๊ณ
return firstName.value + " " + lastName.value;;
},
set(value) {
//๋ฐ๊ณ
},
});
console.log("console ์ถ๋ ฅ:", fullName.value);
//๋ณ๊ฒฝ๋๋ ๊ฐ
fullName.value = "์ด ์ฌ์ฐ";
return {
fullName,
};
},
};
์ด๋ ๊ฒ ๊ฐ์ return
ํ ์ ์๋ค.
import { computed, ref } from "vue";
export default {
setup() {
const firstName = ref("ํ");
const lastName = ref("๊ธธ๋");
const fullName = computed({
get() {
//์ฃผ๊ณ
return firstName.value + " " + lastName.value;;
},
set(value) {
//๋ฐ๊ณ
console.log("value:", value);
},
});
console.log("console ์ถ๋ ฅ:", fullName.value);
//๋ณ๊ฒฝ๋๋ ๊ฐ
fullName.value = "์ด ์ฌ์ฐ";
return {
fullName,
};
},
};
fullName.value = "์ด ์ฌ์ฐ";
๊ฐ์ด set
ํจ์์ ๋งค๊ฐ๋ณ์๋ฅผ ๋์ด์จ๋ค.
import { computed, ref } from "vue";
export default {
setup() {
const firstName = ref("ํ");
const lastName = ref("๊ธธ๋");
const fullName = computed({
get() {
//์ฃผ๊ณ
return firstName.value + " " + lastName.value;
},
set(value) {
//๋ฐ๊ณ
console.log("value:", value);
//1. firstName๊ณผ lastName์ผ๋ก ๋๋๊ธฐ ์ํด ๊ฐ์ ๋ฐ๊ณ split(๋์ด์ฐ๊ธฐ)๋ก ๊ฐ์ ๋๋๋ค.
console.log(value.split(" "));
//2. ๊ทธ๋ฆฌ๊ณ ๊ตฌ์กฐ๋ถํด ํ ๋นํด์ ์๋ก์ด ๋ณ์ -> ๊ธฐ์กด์ ์๋ ๋ณ์์ ํ ๋น ํ๋ค.
[firstName.value, lastName.value] = value.split(" ");
console.log("firstName:", firstName.value);
console.log("lastName:", lastName.value);
},
});
console.log("console ์ถ๋ ฅ:", fullName.value);
//๋ณ๊ฒฝ๋๋ ๊ฐ
fullName.value = "์ด ์ฌ์ฐ";
return {
fullName,
};
},
};
์ด๋ ๊ฒ computed
์ setter(set)
ํจ์๋ฅผ ํ์ฉํด์ ์ฐ๊ธฐ ๊ฐ๋ฅํ ์์ผ๋ก๋ ๋ง๋ค ์ ์๋ค.