๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Vue

โš“ Vue Slot์œผ๋กœ ํ…Œ์ด๋ธ” ๊ตฌํ˜„

by frontChoi 2022. 5. 31.
๋ฐ˜์‘ํ˜•

๐Ÿš† slot

slot์€ ์ปดํผ๋„ŒํŠธ๋ฅผ ์ถ”์ƒํ™” ์‹œํ‚ค๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š”๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค. ์ถ”์ƒํ™” ์‹œํ‚ด์œผ๋กœ์จ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

์ด๋ฒˆ์—๋Š”  slot์„ ์‚ฌ์šฉํ•˜์—ฌ table์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•ด๋ณด์•˜๋‹ค.

 

 

๐Ÿšฉ table thead slot์œผ๋กœ ๋งŒ๋“ค์–ด๋ณด๊ธฐ

table์˜ thead๋ฅผ ๊ตฌํ˜„ํ•ด๋ณผ ๊ฒƒ์ด๋‹ค. table์€ ์ „์—ญ์—์„œ ์‚ฌ์šฉํ•˜์ง€๋งŒ, thead,tbody๋งŒ ๋‚ด์šฉ์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์ด ์žˆ์–ด์„œ, thead,tbody๋ฅผ ์ถ”์ƒํ™” ํ•ด๋ณผ๊ฒƒ์ด๋‹ค.

//Slot.vue
<template>
    <table>
        <thead>
            <tr>
                <slot name="header"></slot>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</template>

<script>
    export default {};
</script>

<style></style>

์œ„์™€ ๊ฐ™์ด "<slot name="header"></slot>" ์ด ๋ถ€๋ถ„์„ ์ถ”์ƒํ™” ์‹œ์ผœ๋†“์•˜๋‹ค. 

์•„๋ž˜์ฝ”๋“œ๋Š” ๊ตฌํ˜„์—์„œ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋Š”์ง€ ๋ณด์—ฌ์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

<Slot>
	<template #header>
		<th>No</th>
		<th>์ƒํ’ˆ๋ช…</th>
		<th>๊ฐ€๊ฒฉ</th>
	</template>
</Slot>

slot์—์„œ ์„ ์–ธํ•œ name="header"๋ฅผ ๊ตฌํ˜„๋ถ€๋ถ„์—์„œ๋Š” ์œ„์™€ ๊ฐ™์ด #header๋กœ ๊ตฌํ˜„ํ•˜์˜€๊ณ  #header ์ฝ”๋“œ ์•ˆ์— th์— ๋“ค์–ด๊ฐ€๋Š” ๋ถ€๋ถ„์„ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. 

 

 

๐ŸŒ„ table tbody slot์œผ๋กœ ๋งŒ๋“ค๊ธฐ

tbody๋„ thead์™€ ๊ฐ™์ด ์ถ”์ƒํ™”์‹œํ‚ฌ ๊ฒƒ์ด๋‹ค.  ๋จผ์ € ๋ถ€๋ชจํ•œํ…Œ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก props์— tlist๋ฅผ ์„ ์–ธํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  tlist๋ฅผ ์ด์šฉํ•ด ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆด๊ฒƒ์ด๋‹ค. slot์€ ๋ฐ˜๋ณต๋ฌธ์ด ๋˜์ง€ ์•Š์•„ template์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ธ๋‹ค.

๊ทธ๋ฆฌ๊ณ  tr ๋ฐ‘์— slot์„ ์„ ์–ธํ•˜๊ณ , ๋ถ€๋ชจ์—์„œ ์ž์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก :row="item"์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

์‚ฌ์šฉ๋ฐฉ์‹์€ :๋ถ€๋ชจ์—์„œ์‚ฌ์šฉํ• ๋ณ€์ˆ˜="์ž์‹์—์„œ์“ฐ์ด๋Š” ๋ฐ์ดํ„ฐ"  ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

<template>
	<table>
		<thead>
			<tr>
				<slot name="header"></slot>
			</tr>
		</thead>
		<tbody>
			<template v-for="(item, index) in tlist" :key="'item' + index">
				<tr>
					<slot name="list" :row="item"></slot>
				</tr>
			</template>
		</tbody>
	</table>
</template>

<script>
export default {
	props: {
		tlist: {
			type: Array,
			default() {
				return [];
			},
		},
	},
};
</script>

<style></style>

 

์•„๋ž˜์ฝ”๋“œ๋Š” ๊ตฌํ˜„ ๋ถ€๋ถ„์ด๋‹ค.

tbody์˜ slot name์„ list์œผ๋กœ ๋งŒ๋“ค์—ˆ๊ณ , ์ž์‹์˜ ์ปดํผ๋„ŒํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ€๋ชจ์—์„œ ์“ธ ์ˆ˜ ์žˆ๋„๋ก #list="{ row }"๋น„๊ตฌ์กฐํ™”ํ• ๋‹น์„ ํ†ตํ•ด ์“ธ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.

<template>
    <Slot :tlist="tableBody">
        .
        .
        .
        <template #list="{ row }">
            <td>{{ row.item1 }}</td>
            <td>{{ row.item2 }}</td>
            <td>{{ row.item3 }}</td>
        </template>
    </Slot>
</template>

<script>
	setup() {
    	const tableBody = ref([
        	{item1:1, item2:2,item3:3}
        ]);
        
        return{
        	tableBody
        }
    }
</script>

 

 

๐Ÿš‰ ์‚ฌ์šฉํ•˜๊ณ  ๋‚œ ํ›„๊ธฐ

์•„์ง ๊ต‰์žฅํžˆ ๋‚ฎ์€ ๋‹จ๊ณ„์˜ ๊ตฌํ˜„์ด๋ผ ๋งŽ์ด ๋ถ€์กฑํ•œ ์ ๋„ ๋งŽ์œผ๋‚˜, slot์„ ํ†ตํ•ด ์ถ”์ƒํ™”์‹œ์ผœ์„œ ์ปดํผ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ฒŒ ํ–ˆ๋‹ค๋Š” ์žฅ์ ์€ ํ™•์‹คํžˆ ๋А๊ผˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€