react 实现嵌套路由重定向到404页面

react 实现嵌套路由重定向到404页面

  • 每次进行react 路由配置的时候都感觉力不从心 , 大脑内存严重不足 , 特在此记录 , 进行一定的说明, 方便日后参考使用

需求: 二级路由结构 , 404页面 ,

  • 路由的匹配规则:

route 中的 path 与浏览器 hash 进行匹配 , 只要地址栏的 hash 完整包含 path 的值 , 就会加载对应的 path 的路由

举个栗子 (hash忽略#号)

1 . 当路由的path值为 / 或 * 时 , 任何 hash 下都会加载路由

浏览器hash:/home ----- path值: / -----当前路由加载
浏览器hash:/home/中兄次之/秩序册Z从 ----- path值: / -----当前路由加载

2 . 地址栏的 hash 完整包含 path 的值 ,

浏览器hash:/home/123 ----- path值: /home -----当前路由加载
浏览器hash:/home/123 ----- path值: /home/ -----当前路由加载
浏览器hash:/home ----- path值: /home/ -----当前路由加载
浏览器hash:/123/home ----- path值: /home -----当前路由不加载

路由结构

  • 一级路由
    /layout /404
        <Switch> //switch作用: 类似于js的switch,只会选取一个匹配项
          <Route path="/layout" component={Layout}></Route>
          <Route path="/404" component={NotFound}></Route>
          <Redirect exact from="/" to="/layout"></Redirect>  //优化处理
          <Redirect from="*" to="/404" ></Redirect> //最末尾的重定向操作,后面不能插入其他路由
        </Switch>
  • 二级路由
    /layout/home--- /layout/find--- /layout/news--- /layout/profile---
 	<Switch>
         <Route exact path="/layout/home" component={Home}></Route>
         <Route exact path="/layout/news" component={News}></Route>
         <Route exact path="/layout/findhouse/:id?" component={Findhouse}></Route>
         <Route exact path="/layout/profile" component={Profile}></Route>
         <Redirect exact path="/layout" to="/layout/home" ></Redirect> //配合一级路由的优化处理,进入到layout直接重定向到home页
         <Redirect from="*" to="/404"></Redirect>
    </Switch>
React Router是一个React的第三方库,它提供了一种在React应用程序中实现路由的方式。React Router DOM是React Router的一个扩展,它为React应用程序提供了一个DOM导航组件。 React Router DOM的嵌套路由是一种将多个路由组件嵌套在一起的技术。这使得我们可以在一个页面上使用多个路由,每个路由有自己的路径和组件。 下面是React Router DOM 6的嵌套路由使用详细步骤: 1. 安装React Router DOM 使用npm或yarn安装React Router DOM npm install react-router-dom 2. 创建父路由组件 在你的React组件中引入BrowserRouter和Route组件,BrowserRouter是一个Router组件的具体实现,它使用HTML5 history API来实现单页应用程序的路由。 import { BrowserRouter as Router, Route } from 'react-router-dom'; const App = () => { return ( <Router> <Route path="/" component={Home} /> </Router> ); } 3. 创建子路由组件 在父路由组件中,我们可以使用Route组件来定义子路由。使用嵌套路由的一个好处是可以将路由组件分解为更小的组件。在这里,我们将创建一个包含嵌套路由的组件。 import { Route } from 'react-router-dom'; const Profile = () => { return ( <div> <h2>Profile Page</h2> <Route path="/profile/info" component={Info} /> <Route path="/profile/settings" component={Settings} /> </div> ); } 4. 使用嵌套路由 在父路由组件中,我们可以在Route组件中使用嵌套路由。在这个例子中,我们将使用Profile组件作为子路由。 import { BrowserRouter as Router, Route } from 'react-router-dom'; const App = () => { return ( <Router> <Route path="/" component={Home} /> <Route path="/profile" component={Profile} /> </Router> ); } 5. 创建Link组件 在使用嵌套路由的过程中,我们需要创建Link组件来将路由链接到不同的页面。在这里,我们将创建一个Link组件来将路由链接到Profile组件。 import { Link } from 'react-router-dom'; const Home = () => { return ( <div> <h2>Home Page</h2> <Link to="/profile">Go to Profile</Link> </div> ); } 至此,我们已经完成了React Router DOM 6的嵌套路由的使用。在使用React Router DOM 6时,我们需要注意一些变化: - Route组件的component属性已经被替换为element属性,用来指定将要被渲染的React元素。 - Route组件的exact属性默认为true。 - 嵌套路由使用Route组件而不是Switch组件来处理路由匹配问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值