Add 404 Not Found route to the router configuration to handle unmatched paths.

This commit is contained in:
超级老白兔
2025-11-05 10:37:39 +08:00
parent e163ada783
commit fa802b50ad
3 changed files with 144 additions and 1 deletions

View File

@@ -0,0 +1,75 @@
.not-found-container {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
padding: 20px;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}
.not-found-content {
text-align: center;
max-width: 500px;
width: 100%;
}
.error-code {
font-size: 120px;
font-weight: bold;
color: #1890ff;
line-height: 1;
margin-bottom: 20px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
@media (max-width: 768px) {
font-size: 80px;
}
}
.error-title {
font-size: 28px;
font-weight: 600;
color: #333;
margin: 20px 0 16px;
@media (max-width: 768px) {
font-size: 24px;
}
}
.error-description {
font-size: 16px;
color: #666;
margin-bottom: 40px;
line-height: 1.6;
@media (max-width: 768px) {
font-size: 14px;
margin-bottom: 30px;
}
}
.action-buttons {
display: flex;
gap: 16px;
justify-content: center;
flex-wrap: wrap;
@media (max-width: 768px) {
flex-direction: column;
gap: 12px;
}
}
.action-btn {
display: flex;
align-items: center;
gap: 8px;
min-width: 140px;
@media (max-width: 768px) {
width: 100%;
justify-content: center;
}
}

View File

@@ -0,0 +1,59 @@
import React from "react";
import { useNavigate } from "react-router-dom";
import { Button } from "antd-mobile";
import { ArrowLeftOutlined, HomeOutlined } from "@ant-design/icons";
import Layout from "@/components/Layout/Layout";
import styles from "./index.module.scss";
const NotFound: React.FC = () => {
const navigate = useNavigate();
const handleGoHome = () => {
navigate("/");
};
const handleGoBack = () => {
navigate(-1);
};
return (
<Layout>
<div className={styles["not-found-container"]}>
<div className={styles["not-found-content"]}>
{/* 404 图标 */}
<div className={styles["error-code"]}>404</div>
{/* 错误提示 */}
<h1 className={styles["error-title"]}></h1>
<p className={styles["error-description"]}>
访
</p>
{/* 操作按钮 */}
<div className={styles["action-buttons"]}>
<Button
color="primary"
size="large"
onClick={handleGoHome}
className={styles["action-btn"]}
>
<HomeOutlined />
<span></span>
</Button>
<Button
color="default"
size="large"
onClick={handleGoBack}
className={styles["action-btn"]}
>
<ArrowLeftOutlined />
<span></span>
</Button>
</div>
</div>
</div>
</Layout>
);
};
export default NotFound;

View File

@@ -1,6 +1,7 @@
import React from "react";
import { BrowserRouter, useRoutes, RouteObject } from "react-router-dom";
import PermissionRoute from "./permissionRoute";
import NotFound from "@/pages/404";
// 动态导入所有 module 下的 ts/tsx 路由模块
const modules = import.meta.glob("./module/*.{ts,tsx}", { eager: true });
@@ -31,7 +32,15 @@ function wrapWithPermission(
return route;
}
const routes = allRoutes.map(wrapWithPermission);
// 添加 404 路由(通配符路由,必须放在最后)
const routes = [
...allRoutes.map(wrapWithPermission),
{
path: "*",
element: <NotFound />,
auth: false,
},
];
const AppRoutes = () => useRoutes(routes);