Next.js 跳转到 not-found.tsx 和 error.tsx 定义的页面

Next.js About 686 words

not-found

跳转到not-found.tsx定义的页面。

使用notFound()方法即可。无需return

import {notFound} from "next/navigation";

export default async function Page({
                                       params,
                                   }: {
    params: Promise<{ id: string }>
}) {
    if (xxx) {
        notFound();
    }
    return (
        <></>
    )
}

error

跳转到error.tsx定义的页面。

异常情况中直接throw抛出Error即可,Next.js会捕获异常并定向到error.tsx

export default async function Page({
                                       params,
                                   }: {
    params: Promise<{ id: string }>
}) {
    if (xxx) {
        throw new Error('id must be a number')
    }
    return (
        <></>
    )
}
Views: 13 · Posted: 2025-10-15

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓
Today In History
Browsing Refresh