1、安装依赖
yarn add react-router-dom
2、创建路由文件router.js
import React, { Component } from 'react'; import { BrowserRouter as Router, Route } from 'react-router-dom'; import login from '../pages/login/login'; class RouteConfig extends Component { render() { return ( <Router> {/* 配置首页 */} <Route path="/" exact component={login} /> <Route path="/login/" component={login} /> </Router> ); } } export default RouteConfig;
3、index.js引入router.js
import React from 'react'; import ReactDOM from 'react-dom'; import Route from './router/router'; import * as serviceWorker from './serviceWorker'; const render = (Component) => { ReactDOM.render( <Component />, document.getElementById('root'), ); }; render(Route); serviceWorker.unregister();
本文作者为gengboxb,转载请注明。