BlogPosts
BlogPosts (블로그 게시글 목록)
BlogPosts는 반응형 그리드 레이아웃으로 블로그 게시글 목록을 표시하는 컴포넌트예요. 게시글을 세로·가로로 깔끔하게 배열해서 블로그 페이지를 쉽게 구성할 수 있어요.
출처: Nuxt UI
본문
BlogPosts 컴포넌트는 기본 슬롯(default slot)이나 posts prop을 사용해 BlogPost 컴포넌트 목록을 표시하는 유연한 레이아웃을 제공해요.
<template>
<UBlogPosts>
<UBlogPost
v-for="(post, index) in posts"
:key="index"
v-bind="post"
/>
</UBlogPosts>
</template>
게시글 (Posts)
posts prop에 BlogPost 컴포넌트의 속성들을 담은 객체 배열을 전달하면 돼요.
<script setup lang="ts">
import type { BlogPostProps } from '@nuxt/ui'
const posts = ref<BlogPostProps[]>([
{
title: "Nuxt Icon v1",
description: "Discover Nuxt Icon v1!",
image: "https://nuxt.com/assets/blog/nuxt-icon/cover.png",
date: "2024-11-25"
},
{
title: "Nuxt 3.14",
description: "Nuxt 3.14 is out!",
image: "https://nuxt.com/assets/blog/v3.14.png",
date: "2024-11-04"
},
{
title: "Nuxt 3.13",
description: "Nuxt 3.13 is out!",
image: "https://nuxt.com/assets/blog/v3.13.png",
date: "2024-08-22"
}
])
</script>
<template>
<UBlogPosts :posts="posts" />
</template>
방향 (Orientation)
orientation prop을 사용해 BlogPosts의 방향을 변경할 수 있어요. 기본값은 horizontal이에요.
<script setup lang="ts">
import type { BlogPostProps } from '@nuxt/ui'
const posts = ref<BlogPostProps[]>([
{
title: "Nuxt Icon v1",
description: "Discover Nuxt Icon v1!",
image: "https://nuxt.com/assets/blog/nuxt-icon/cover.png",
date: "2024-11-25"
},
{
title: "Nuxt 3.14",
description: "Nuxt 3.14 is out!",
image: "https://nuxt.com/assets/blog/v3.14.png",
date: "2024-11-04"
},
{
title: "Nuxt 3.13",
description: "Nuxt 3.13 is out!",
image: "https://nuxt.com/assets/blog/v3.13.png",
date: "2024-08-22"
}
])
</script>
<template>
<UBlogPosts orientation="vertical" :posts="posts" />
</template>
[!TIP]
기본 슬롯 대신
postsprop을 사용하면 게시글의orientation이 자동으로 반전돼요. 즉horizontal은vertical로, 그 반대도 마찬가지예요.
예제 (Examples)
[!NOTE]
다음 예제들은 Nuxt Content를 사용하지만, 이 컴포넌트들은 어떤 콘텐츠 관리 시스템과도 함께 사용할 수 있어요.
페이지 안에서 사용하기
페이지에서 BlogPosts 컴포넌트를 사용해 블로그 페이지를 만들 수 있어요:
<script setup lang="ts">
const { data: posts } = await useAsyncData('posts', () => queryCollection('posts').all())
</script>
<template>
<UPage>
<UPageHero title="Blog" />
<UPageBody>
<UContainer>
<UBlogPosts>
<UBlogPost
v-for="(post, index) in posts"
:key="index"
v-bind="post"
:to="post.path"
/>
</UBlogPosts>
</UContainer>
</UPageBody>
</UPage>
</template>
[!NOTE]
이 예제에서는
@nuxt/content모듈의queryCollection을 사용해posts를 가져와요.
[!TIP]
@nuxt/content는path속성을 사용하므로 여기서는toprop을 덮어썼어요.
API
Props
/**
* Props for the BlogPosts component
*/
interface BlogPostsProps {
/**
* The element or component this component should render as.
* @default 'div'
*/
as?: any;
posts?: BlogPostProps[] | undefined;
/**
* The orientation of the blog posts.
* @default 'horizontal'
*/
orientation?: "horizontal" | "vertical" | undefined;
ui?: { base?: any; } | undefined;
}
Slots
/**
* Slots for the BlogPosts component
*/
interface BlogPostsSlots {
date(): any;
badge(): any;
title(): any;
description(): any;
authors(): any;
header(): any;
body(): any;
footer(): any;
default(): any;
}
테마 (Theme)
export default defineAppConfig({
ui: {
blogPosts: {
base: 'flex flex-col gap-8 lg:gap-y-16',
variants: {
orientation: {
horizontal: 'sm:grid sm:grid-cols-2 lg:grid-cols-3',
vertical: ''
}
}
}
}
})