๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
[๊ฐœ๋ฐœ] Practice/Vue.js

[Vue.js] props ๋Œ€์‹  slot ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

by Connecting-the-dots 2022. 4. 7.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ก ์‹ค์Šต ํฌ์ธํŠธ!

  • slot ์„ ์‚ฌ์šฉํ•ด์„œ ํ•„ํ„ฐ๊ฐ€ ์”Œ์›Œ์ง„ ์ด๋ฏธ์ง€๋“ค์— ๊ฐ๊ฐ์— ํ•ด๋‹นํ•˜๋Š” ํ•„ํ„ฐ ์ด๋ฆ„์„ ๋ถ™์—ฌ๋ณด์•˜๋‹ค.
  • ๊ธฐ์กด์— ๋งŒ๋“ค์–ด๋†“์€ ๋ ˆ์ด์•„์›ƒ์— slot ๋งŒ ์ถ”๊ฐ€ํ–ˆ๋”๋‹ˆ ํ•„ํ„ฐ๊ฐ€ ์”Œ์›Œ์ง„ ์ด๋ฏธ์ง€ ์œ„์— ํ•„ํ„ฐ ์ด๋ฆ„์ด ๊ฒน์ณ๋‚˜์™€์„œ FilterBox.vue ํŒŒ์ผ์˜ ๋ ˆ์ด์•„์›ƒ์„ ์ „๋ฐ˜์ ์œผ๋กœ ์ˆ˜์ •ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ, ์ž‘์€ ์ด๋ฏธ์ง€๋“ค์— ํ•„ํ„ฐ๊ฐ€ ์ ์šฉ์ด ์•ˆ๋˜๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ–ˆ๋Š”๋ฐ, ์ด๋Š” Container.vue ์—์„œ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆด ๋•Œ filter ์š”์†Œ๋“ค์„ data ๋ณด๊ด€ํ•จ์— ์ €์žฅํ–ˆ๋‹ค๊ฐ€ props ๋ฌธ๋ฒ•์œผ๋กœ FilterBox.vue ๋กœ ๋ฐ›์•„์™€์„œ ๋“ฑ๋กํ•œ ๋’ค ์‚ฌ์šฉํ•˜๋‹ˆ ์ •์ƒ์ ์œผ๋กœ ํ•„ํ„ฐ๊ฐ€ ๊ฐ ์ด๋ฏธ์ง€์— ์ ์šฉ์ด ๋˜์—ˆ๋‹ค.
    (์ดํ›„, App.vue ์—์„œ๋„ filter ํ•ญ๋ชฉ์„ ์‚ฌ์šฉํ•  ์ผ์ด ์ƒ๊ฒจ ๊ธฐ์กด์˜ Container.vue ์— ์žˆ๋˜ data ๋ณด๊ด€ํ•จ์˜ filter ํ•ญ๋ชฉ์„ App.vue ๋กœ ์˜ฎ๊ฒจ์ฃผ๊ณ  ๋ชจ๋“  ํ•˜์œ„ Component ์—๋Š” props ๋ฌธ๋ฒ•์„ ์ด์šฉํ•˜์—ฌ ์ „์†กํ•ด์ฃผ์—ˆ๋‹ค.)
  • slot ์„ ํ†ตํ•ด ์ ์šฉํ•œ ํ•„ํ„ฐ ์ด๋ฆ„์˜ ๊ฒฝ์šฐ ๊ธฐ๋ณธ์ ์œผ๋กœ ์™ผ์ชฝ ์ •๋ ฌ์ด์—ˆ๋Š”๋ฐ, <slot></slot> ์€ ์ด ์ž์ฒด๋กœ css ์Šคํƒ€์ผ๋ง์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ <div></div> ์— ๋‹ด์•„์„œ ๊ฐ€์šด๋ฐ ์ •๋ ฌ์„ ํ•ด์ฃผ์—ˆ๋‹ค.

๐Ÿ’œ ๋“ค์–ด๊ฐ€๊ธฐ ์ „์—

<div :class="๋ฐ์ดํ„ฐ" class="filter-item"> </div>
  • Vue ์—์„œ๋Š” ์œ„์™€ ๊ฐ™์ด html ํƒœ๊ทธ ์•ˆ์— class="" ํ˜•์‹์œผ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ’œ slot ๋ฌธ๋ฒ• ์•Œ์•„๋ณด๊ธฐ

  • ์ž์‹ Component ๊ฐ€ ๋ถ€๋ชจ Component ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด ์ผ๋ฐ˜์ ์œผ๋กœ props ๋กœ ์ „์†ก๋ฐ›์•„์•ผ ํ•œ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ์ „์†ก๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋“ฑ๋กํ•ด์•ผ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์œ„์™€ ๊ฐ™์ด props ๋ฌธ๋ฒ•์˜ ์‚ฌ์šฉ์ด ๊ท€์ฐฎ์„ ๋•Œ ๋Œ€์šฉ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„ํŽธํ•œ ๋ฌธ๋ฒ•์ด ๋ฐ”๋กœ  slot ๋ฌธ๋ฒ•์ด๋‹ค.

๐Ÿค slot ๋ฌธ๋ฒ• ์‚ฌ์šฉํ•˜๊ธฐ

  • ์ž์‹ Component ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐˆ ์œ„์น˜๋ฅผ ์ •ํ•ด์„œ <slot></slot> ์ด๋ผ๋Š” html ํƒœ๊ทธ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  • ๋ถ€๋ชจ Component ์—์„œ <์ž์‹ Component></์ž์‹ Component> ์‚ฌ์ด์— ์ž์‹ Component ์— ์ „์†กํ•  ๋ฐ์ดํ„ฐ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.
  • ์œ„์˜ ๊ณผ์ •์„ ๊ฑฐ์น˜๋ฉด ๋”ฐ๋กœ ๋“ฑ๋กํ•˜๋Š” ๊ณผ์ • ํ•„์š”์—†์ด ๋ถ€๋ชจ Component ๊ฐ€ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๊ฐ€ ์ž์‹ Component ๋‚ด์˜ <slot></slot> ์‚ฌ์ด์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.
  • ๋‹ค๋งŒ, props ๋Š” src ๋‚˜ style ์†์„ฑ์—๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, slot ์€ ๋”ฑ html ํƒœ๊ทธ ์‚ฌ์ด์— ๋‚ด์šฉ์„ ์ž‘์„ฑํ•˜๋“ฏ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿค Named Slots

  • ๋ถ€๋ชจ Component ์—์„œ ์ „์†กํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ๋งŽ์•„์„œ ์—ฌ๋Ÿฌ๊ฐœ์˜ <slot></slot> ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ๊ฐ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด name ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ Named Slots ์ด๋ผ๊ณ  ํ•œ๋‹ค.

 

<slot name="a"></slot>
  • ์ž์‹ Component ๋Š” ์œ„์™€ ๊ฐ™์ด name ์„ ์›ํ•˜๋Š” ๋Œ€๋กœ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

<์ž์‹ Component>
  <template v-slot:a>๋ฐ์ดํ„ฐ</template>
</์ž์‹ Component>
  • ๊ทธ๋Ÿฌ๋ฉด ๋ถ€๋ชจ Component ์—์„œ๋Š” <์ž์‹ Component></์ž์‹ Component> ์‚ฌ์ด์— <template v-slot:์ž์‹_Component์—_์ ์€_์ด๋ฆ„>์ „์†กํ• _๋ฐ์ดํ„ฐ</template> ์˜ ํ˜•์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ํŠน์ • ์ž์‹ Component ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿค Slot Props

  • ๋ถ€๋ชจ Component ๋„ ๊ฐ€๋” ์ž์‹ Component ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ทธ๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด Slot Props ์ด๋‹ค.

 

<slot :๋ฐ์ดํ„ฐ="๋ฐ์ดํ„ฐ"></slot>
  • ์ž์‹ Component ๋Š” slot ์„ ์‚ฌ์šฉํ•  ๋•Œ ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ€๋ชจ Component ๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

 

<์ž์‹ Component>
  <template v-slot:default="์ž‘๋ช…"> {{์ž‘๋ช….๋ฐ์ดํ„ฐ}}</template>
</์ž์‹ Component>
  • ์ด์ œ ๋ถ€๋ชจ Component ๊ฐ€ ์ž์‹ Component ๋กœ๋ถ€ํ„ฐ ์ „์†ก๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด, v-slot: ์˜†์— default ์™€ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋“ค์„ ํ†ต์นญํ•  ์ด๋ฆ„์„ ์ง€์ •ํ•ด์„œ ๊ฐ™์ด ์ ์–ด์ค€๋‹ค.
  • ์ด์ œ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋Š” ์ง€์ •ํ•œ ์ด๋ฆ„์— ์ €์žฅ์ด ๋˜๊ณ  ์ด๋Š” object ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด์–ด ์‚ฌ์šฉํ•  ๋•Œ์—๋„ object ํƒ€์ž…์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด๋“ฏ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

728x90
๋ฐ˜์‘ํ˜•