react如何使用路由、路由使用方法
侧边栏壁纸
  • 累计撰写 635 篇文章
  • 累计收到 0 条评论

react如何使用路由、路由使用方法

加速器之家
2024-08-22 / 0 评论 / 3 阅读 / 正在检测是否收录...

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即可访问

http://localhost:3000/#/login

0

评论

博主关闭了当前页面的评论