網站首頁 編程語言 正文
1 - 單級路由
Home about ? }/> ?
2 - 嵌套路由(about路徑進行嵌套)
一級路由
Home about ? }/> ? ?
二級路由
跳轉按鈕一般最好寫完整,否則看代碼看不清楚
新聞 消息 ? ? }/> ? ? }/>
跳轉切換的path要寫二級路由的路徑
React Router v6使用路由嵌套和重定向
v6版本與v5版本差異較大,以下為V6使用路由嵌套及路由重定向方法,子路由路徑不用加"/",v6中會自動拼接
App.jsx
import React, { Component } from 'react' import { Route,Routes,Navigate} from 'react-router-dom' import About from './pages/About' import Home from './pages/Home' import Header from './components/Header' import MyNavLink from './components/MyNavLink' export default class App extends Component { ? ? render() { ? ? ? ?? ? ? ? ? return ( ? ? ? ? ? ?? ? ? ? ? ? ?? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ) ? ? } }? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? {/* 鏈接路由 */} ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ?About ? ? ? ? ? ? ? ? ? ? ? ? ? ?Home ? ?? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? {/* 注冊路由 */} ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ??? ?{/* 首次進入頁面是重定向到/about路徑 */} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}>?? ??? ??? ??? ??? ??? ??? ??? ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Home/index.jsx
import React, { Component } from 'react' import MyNavLink from '../../components/MyNavLink' import {Route,Routes,Navigate} from 'react-router-dom' import News from './News' import Messages from './Messages' export default class Home extends Component { ? ? render() { ? ? ? ? return ( ? ? ? ? ? ?? ? ? ? ? ? ? ?? ? ? ? ) ? ? } }Home內容
? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ?News ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Messages ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ?? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}> ? ? ? ? ? ? ? ? ? ? ? ? }> ? ? ? ? ? ? ? ? ? ? ? ?}> ? ? ? ? ? ? ? ? ? ?
Home/Messages/index.jsx
import React, { Component } from 'react' export default class Messages extends Component { ? ? render() { ? ? ? ? return ( ? ? ? ? ? ?
-
? ? ? ? ? ? ? ?
- message1 ? ? ? ? ? ? ? ?
- message2 ? ? ? ? ? ? ? ?
- message3 ? ? ? ? ? ? ? ?
- message4 ? ? ? ? ? ?
MyNavLink/index.jsx
import React, { Component } from 'react' import { NavLink } from 'react-router-dom' export default class MyVavLink extends Component { ? ? render() { ? ? ? ? return ( ? ? ? ? ? ?? ? ? ? ) ? ? } }
原文鏈接:https://blog.csdn.net/led1114/article/details/122802885
相關推薦
- 2023-05-20 Android內存泄漏導致原因深入探究_Android
- 2022-04-20 Python全棧之模板渲染詳解_python
- 2022-04-17 算法時間復雜度和空間復雜度
- 2022-10-06 Python時間戳與日期格式之間相互轉化的詳細教程_python
- 2022-09-13 C語言如何實現翻轉字符串中的單詞_C 語言
- 2022-10-18 Qt實現TCP客戶端和服務器通訊程序_C 語言
- 2022-06-26 Android開發快速實現底部導航欄示例_Android
- 2022-10-02 pandas數據類型之Series的具體使用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支