1、 首先安装路由npm i react-router-dom插件,已安装的可以跳过
npm i react-router-dom
2、打开项目下src\index.js文件,添加如下代码
import {HashRouter as Router, Switch, Route} from 'react-router-dom'; import Login from './pages/Login/Login'; import Wecome from './pages/Home/Wecome';
3、 路由部分代码,path为路径,component为组件地址
ReactDOM.render( <Router> <Switch> <Route path="/login" component={Login}/> <Route path="/wecome" component={Wecome}/> </Switch> </Router>, document.getElementById('root') );
4、 完全index.js路由代码
import React from 'react'; import ReactDOM from 'react-dom'; import {HashRouter as Router, Switch, Route} from 'react-router-dom'; import './index.css'; import reportWebVitals from './reportWebVitals'; import Login from './pages/Login/Login'; import Wecome from './pages/Home/Wecome'; ReactDOM.render( <Router> <Switch> <Route path="/login" component={Login}/> <Route path="/wecome" component={Wecome}/> </Switch> </Router>, 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();
5、 访问:输入下面url即可访问
文章评论(0)