본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

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]

기본 슬롯 대신 posts prop을 사용하면 게시글의 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 속성을 사용하므로 여기서는 to prop을 덮어썼어요.

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: ''
        }
      }
    }
  }
})

변경 로그 (Changelog)

컴포넌트와 테마의 커밋 기록을 확인해 보세요.

더 알아보기 (Learn more)