V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
Deshun
V2EX  ›  程序员

Astro 多层目录报错,有大佬知道怎么解决吗

  •  
  •   Deshun · Dec 14, 2023 · 1810 views
    This topic created in 865 days ago, the information mentioned may be changed or developed.

    Astro 项目,比如我的 blog 分类下再分日期存放文章,编译时就会报错: Expected "slug" to match "[^\/#\?]+?", but got "2023-12-13/text"

    文件位置:/src/content/blog/2023-12-13/text.md

    如果全部放在 /src/content/blog/ 目录下就不会有问题。

    有熟悉 Astro 的大佬知道怎么解吗?

    6 replies    2023-12-14 11:10:32 +08:00
    blankmiss
        1
    blankmiss  
       Dec 14, 2023
    Astro 是什么 下面地址也不贴一个吗
    blankmiss
        2
    blankmiss  
       Dec 14, 2023
    下面地址 --> 项目地址
    LavaC
        3
    LavaC  
       Dec 14, 2023
    这么放是没问题的,应该是你其它地方写错了,看看[...slug].astro
    Deshun
        4
    Deshun  
    OP
       Dec 14, 2023
    @LavaC [...slug].astro 是在 /blog 下面:
    ```
    ---
    export const prerender = true

    import { getEntryBySlug, getCollection } from "astro:content";
    import PostLayout from '$/layouts/post.astro';

    export async function getStaticPaths() {
    const allPosts = await getCollection('blog');
    return allPosts.map(post => ({
    params: {
    slug: post.slug
    }
    }))
    }

    const { slug } = Astro.params
    const entry = await getEntryBySlug('blog', slug!)

    const { Content } = await entry.render()
    ---
    <PostLayout slug={slug} content={entry.data} >
    <Content/>
    </PostLayout>
    ```
    LavaC
        5
    LavaC  
       Dec 14, 2023
    @Deshun 怪了,参照 issue[#7121]( https://github.com/withastro/astro/issues/7121),同样的代码我也只会在文件叫[slug]而非[...slug]时编译报错。
    Deshun
        6
    Deshun  
    OP
       Dec 14, 2023
    @LavaC 非常感谢,问题解决。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4469 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 04:09 · PVG 12:09 · LAX 21:09 · JFK 00:09
    ♥ Do have faith in what you're doing.