安装 React 路由依赖
npm inistall react-router react-router-dom -S
配置路由懒加载
index.jsx
import React from 'react';
import React from 'react-dom';
import App from './App';
import {BrowserRouter} from './react-router-dom';
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root')
);
App.jsx
import React, {Component, lazy, Suspense} from 'react';
import Loading from './loading';
import {Switch, Route} from 'react-router';
class App extends Component{
render(){
return (
<Suspense fallback={<Loading />}>
<div className="app">
<Switch>
<Route path="/page1" component={lazy(() => import('./views/Page1'))}/>
<Route path="/page2" component={lazy(() => import('./views/Page2'))}/>
<Route path="/page3" component={lazy(() => import('./views/Page3'))}/>
</Switch>
</div>
</Suspense>
)
}
}