admin

Astro中getStaticPaths和posts类型问题导致的渲染错误排查?

悬赏1金钱未解决
  1. ---
  2. import BaseLayout from "../../layouts/BaseLayout.astro";

  3. export async function getStaticPaths() {
  4.   const allPosts = await Astro.glob('../posts/*.md');

  5.   const uniqueTags = [...new Set(allPosts.map((post) => post.frontmatter.tags).flat())];

  6.   return uniqueTags.map((tag) => {
  7.     const filteredPosts = allPosts.filter((post) => post.frontmatter.tags.includes(tag));
  8.     return {
  9.       params: { tag },
  10.       props: { posts: filteredPosts },
  11.     };
  12.   });
  13. }

  14. const { tag } = Astro.params;
  15. const { posts } = Astro.props;
  16. ---

  17. <BaseLayout>

  18.   <p>包含「{tag}」标签的文章</p>
  19.   <ul>
  20.     {posts.map((post) => <li><a href={post.url}>{post.frontmatter.title}</a></li>)}
  21.   </ul>

  22. </BaseLayout>
复制代码


暂无评论

写下一个评论

您需要登录后才可以回帖 登录 | 立即注册

Cpoyright © 2021-2025 Discuz! X 版权所有 All Rights Reserved.

Powered by Discuz! X5.1

在本版发帖
返回顶部
快速回复 返回顶部 返回列表