首先在src目录下创建routes文件夹
然后引入Navigate模块和响应的页面
import {Navigate } from 'react-router-dom'; // 路由插件
import HOME from "../pages/home";
import ABOUT from "../pages/about";
const indexRouter = [
{
path:'/home',
element:<HOME />
},
{
path:'/about',
element:<ABOUT />
},
{
path:'/',
element:<Navigate to='/home' />
}
]
export default indexRouter
然后在app.js创建useRoutes模块用于创建路由表,在引入routes文件
// App.js
import './App.css';
import { NavLink, useRoutes } from 'react-router-dom'; // 路由插件
import routes from './routes'
function App() {
const element = useRoutes(routes)
return (
<div>
<NavLink to='/home'>首页</NavLink>
<NavLink to='/about'>分类</NavLink>
<br />
{element}
</div >
);
}
export default App;