[Vue]Computed Properties

chaewonยท2024๋…„ 4์›” 12์ผ
0
post-thumbnail

โœ๐Ÿป Computed

computed

  • ํ…œํ”Œ๋ฆฟ ๋ฌธ๋ฒ•{{ }}์€ ๊ฐ„๋‹จํžˆ ์‚ฌ์šฉํ•˜๋ฉด ๋งค์šฐ ํŽธ๋ฆฌํ•˜๋‹ค. ํ•˜์ง€๋งŒ ํ…œํ”Œ๋ฆฟ ํ‘œํ˜„์‹ ๋‚ด ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง€๋ฉด ๊ฐ€๋…์„ฑ์ด ๋งค์šฐ ๋–จ์–ด์ง„๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์„ ๋•Œ
<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)๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ์‹œ์ ์ด๋‹ค.


Writable Computed

  • Computed๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ getter์ „์šฉ์ด๋‹ค. ๊ณ„์‚ฐ๋œ ์†์„ฑ์— ์ƒˆ ๊ฐ’์„ ํ• ๋‹นํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋Ÿฐํƒ€์ž„ ๊ฒฝ๊ณ ๊ฐ€ ๋œฌ๋‹ค. ์˜ˆ์ œ๋ฅผ ๋ด๋ณด์ž
<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)ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์„œ ์“ฐ๊ธฐ ๊ฐ€๋Šฅํ•œ ์†์œผ๋กœ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

profile
์˜๋ฌธ์€ '์‚ถ์˜ ์ˆ˜์ค€'์„ ๊ฒฐ์ •ํ•˜๊ณ , ์งˆ๋ฌธ์€ '์‚ถ ์ž์ฒด'๋ฅผ ๋ฐ”๊พผ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€