react路由重定向
时间: 2023-09-04 16:06:23 浏览: 267
React路由重定向是通过使用Redirect组件来实现的。在React中,我们可以使用render属性来指定要渲染的内容。通过将Redirect组件放置在一个Route组件内部,并将to属性设置为要重定向的路由地址,就可以实现路由重定向的功能。例如,在下面的代码中,当访问根路径"/"时,将会进行路由重定向,跳转到"/home"路由。
```javascript
import { BrowserRouter as Router, Route, Redirect } from "react-router-dom";
import Home from "./pages/Home";
import CityList from "./pages/CityList";
function App() {
return (
<Router>
<div className="App">
{/* 默认路由,实现路由的重定向 */}
<Route exact path="/" render={() => <Redirect to="/home" />} />
<Route path="/home" component={Home} />
<Route path="/citylist" component={CityList} />
</div>
</Router>
);
}
export default App;
```
这样,当进入页面时,默认路由会重定向到"/home",然后匹配到Home组件进行渲染。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* [react中路由重定向](https://blog.csdn.net/weixin_43131046/article/details/120565092)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *3* [<React-Router>超全react路由知识点(跳转、传参、子路由、重定向等)](https://blog.csdn.net/m0_48465196/article/details/129184085)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文