react-router-config 使用
第一步安装 react-router-config (需要注意不支持 react-router-dom 5以上的版本 )
yarn add react-router-config
第二步在 react-app-env.d.ts 中声明引入
///declare module 'react-router-config'; declare module 'react-router-dom';
第三步 创建 router 文件
import Home from '../pages/Home';
import Login from '../pages/Login';
const routes: any = [ { path: '/', exact: true, component: Home, routes: [] }, { path: '/home', component: Home, routes: [] }, { path: '/login', component: Login, routes: [] } ] export default routes
第四步在 App.tsx 中引入使用
import { BrowserRouter } from 'react-router-dom';
import { renderRoutes } from "react-router-config";
import routes from "./router";
function App() {
return (
{ renderRoutes(routes) }
);
}
export default App;
第五步在 index.tsx 中展示 App 组件
import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import App from './App'; // 性能检测工具 import reportWebVitals from './reportWebVitals'; ReactDOM.render(, document.getElementById('root') ); // If you want to start measuring performance in your app, pass a function // to log results (for example: reportWebVitals(console.log)) // or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals reportWebVitals();
完成以上步骤就能在浏览器上自由切换了