๐ 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์ ํตํด ์ถ์ํ์์ผ์ ์ปดํผ๋ํธ๋ฅผ ์ฌ์ฌ์ฉํ๊ฒ ํ๋ค๋ ์ฅ์ ์ ํ์คํ ๋๊ผ๋ค.
'Vue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๐ vuex ๋ชจ๋ํ (0) | 2023.01.18 |
|---|---|
| ๐ vee-validate ์ ์ฉ (0) | 2022.08.07 |
| ๐ค vue auto routing + ๋ค๋น๊ฒ์ด์ ๊ฐ (0) | 2022.05.05 |
| Vue + eslint + prettier (0) | 2022.02.24 |
| ๐ชeslint + prettier๋ฅผ Vue์ ์ ์ฉํ๊ธฐ (2) | 2021.08.16 |
๋๊ธ