项目地址
创建项目 1 create-react-app react-multiple-page-router
打包环境配置 1 yarn add customize-cra react-app-rewired -D
修改 package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 "scripts" : { "start" : "react-scripts start" , "build" : "react-scripts build" , "test" : "react-scripts test" , "eject" : "react-scripts eject" }, "scripts" : { "start" : "react-app-rewired start" , "build" : "react-app-rewired build" , "test" : "react-app-rewired test" , "eject" : "react-app-rewired eject" },
添加 config-overrides.js
1 2 3 4 5 const { override } = require ("customize-cra" );const overrideConfig = ( ) => (config ) => { return config } module .exports = override (overrideConfig ())
多页面配置 在 src
目录下新建 pages
文件夹用来存放多页面入口
在 pages
目录下新建 pages1
, pages2
目录,并创建入口文件
引入 HtmlWebpackPlugin
组件
1 yarn add html-webpack-plugin -D
修改 config.overrides.js
配置多页面打包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 const { override } = require ("customize-cra" );const htmlWebpackPlugin = require ("html-webpack-plugin" )const overrideConfig = ( ) => (config ) => { config.entry = { main : "./src/index.js" , page1 : "./src/pages/page1/index.js" , page2 : "./src/pages/page2/index.js" } config.output = { filename : "[name].[fullhash].js" , path : __dirname + '/dist' , } config.plugins .push ( new htmlWebpackPlugin ({ title : "main" , template : "./public/index.html" , filename : "main.html" , chunks : ["main" ] }), new htmlWebpackPlugin ({ title : "Page1" , template : "./public/index.html" , filename : "page1.html" , chunks : ["page1" ] }), new htmlWebpackPlugin ({ title : "Page2" , template : "./public/index.html" , filename : "page2.html" , chunks : ["page2" ] }) ) return config } module .exports = override (overrideConfig ())
路由配置 引入 react-router-dom
1 yarn add react-router-dom -D
在 pages1
,pages2
页面添加路由组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import React from 'react' ;import ReactDOM from 'react-dom/client' ;import { BrowserRouter } from 'react-router-dom' ;import './index.css' ;import App from './App' ;import reportWebVitals from '../../reportWebVitals' ;const root = ReactDOM .createRoot (document .getElementById ('root' ));root.render ( <React.StrictMode > <BrowserRouter > <App /> </BrowserRouter > </React.StrictMode > ); reportWebVitals ();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import './App.css' ;import { Routes , Route , NavLink } from "react-router-dom" import Test from '../../components/Test' ;function App ( ) { return ( <div className ="App" > <h1 > Page2</h1 > <NavLink to ={ "/test "}> Test</NavLink > <Routes > <Route path ="/test" element ={ <Test /> } /> </Routes > </div > ); } export default App ;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import React from 'react' ;import ReactDOM from 'react-dom/client' ;import { BrowserRouter } from 'react-router-dom' ;import './index.css' ;import App from './App' ;import reportWebVitals from '../../reportWebVitals' ;const root = ReactDOM .createRoot (document .getElementById ('root' ));root.render ( <React.StrictMode > <BrowserRouter > <App /> </BrowserRouter > </React.StrictMode > ); reportWebVitals ();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import './App.css' ;import { Routes , Route , NavLink } from "react-router-dom" import About from '../../components/About' ;import Home from '../../components/Home' ;function App ( ) { return ( <div className ="App" > <h1 > Page2</h1 > <NavLink to ={ "/home "}> Home</NavLink > <NavLink to ={ "/about "}> About</NavLink > <Routes > <Route path ="/home" element ={ <Home /> } /> <Route path ="/about" element ={ <About /> } /> </Routes > </div > ); } export default App ;
这样就可以切换路由了
存在问题
路由跳转后,刷新页面后,可能会无法定位到对应的路由