1.安装router
cnpm install --save react-router
2.安装react-router-dom
cnpm install react-router-dom --save-dev
3.在src下创建route文件夹,文件夹内创建index.js;
import React from 'react'
import {HashRouter,Route,Switch,Redirect} from 'react-router-dom';
import Index from '../index/index'
import ClassCom from '../components/ClassCom'
class RouterConfig extends React.Component{
render() {
return(
<HashRouter>
<Switch>
<Route exact path="/" component={Index} />
<Route exact path="/ClassCom" component={ClassCom} />
</Switch>
</HashRouter>
)
}
}
export default RouterConfig
4.在入口文件(index.js)引入路由配置文件
import React from 'react';
import ReactDOM from 'react-dom';
import RouterConfig from './Router/index';
import reportWebVitals from './reportWebVitals';//导入优化性能组件
ReactDOM.render(
// 提示
<React.StrictMode>
<RouterConfig />
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();//优化性能