feat: 添加自定义错误和404页面组件

- 添加错误组件以显示未处理的错误信息
- 将NotFound组件重命名为NotFoundComponent并更新显示文本为404 - Not Found。
- 使用自定义错误和未找到页面组件替换默认的错误和404提示。
This commit is contained in:
2026-01-18 04:00:08 +08:00
parent 03c6b84a39
commit 7af770adef
3 changed files with 9 additions and 4 deletions

3
src/components/Error.tsx Normal file
View File

@@ -0,0 +1,3 @@
export function ErrorComponent() {
return <div>An unhandled error happened!</div>
}

View File

@@ -1,3 +1,3 @@
export function NotFound() { export function NotFoundComponent() {
return <div>Not Found</div> return <div>404 - Not Found</div>
} }

View File

@@ -1,6 +1,8 @@
import { TanStackDevtools } from '@tanstack/react-devtools' import { TanStackDevtools } from '@tanstack/react-devtools'
import { createRootRoute, HeadContent, Scripts } from '@tanstack/react-router' import { createRootRoute, HeadContent, Scripts } from '@tanstack/react-router'
import type { ReactNode } from 'react' import type { ReactNode } from 'react'
import { ErrorComponent } from '@/components/Error'
import { NotFoundComponent } from '@/components/NotFount'
import { devtools as queryDevtools } from '@/integrations/tanstack-query' import { devtools as queryDevtools } from '@/integrations/tanstack-query'
import { devtools as routerDevtools } from '@/integrations/tanstack-router' import { devtools as routerDevtools } from '@/integrations/tanstack-router'
import appCss from '@/styles.css?url' import appCss from '@/styles.css?url'
@@ -27,8 +29,8 @@ export const Route = createRootRoute({
], ],
}), }),
shellComponent: RootDocument, shellComponent: RootDocument,
errorComponent: () => <div>Error</div>, errorComponent: () => <ErrorComponent />,
notFoundComponent: () => <div>404 - Not Found</div>, notFoundComponent: () => <NotFoundComponent />,
}) })
function RootDocument({ children }: Readonly<{ children: ReactNode }>) { function RootDocument({ children }: Readonly<{ children: ReactNode }>) {