react怎么配置404页面路由 - 网站

react怎么配置404页面路由

分类:React入门教程_React框架基础教程 · 发布时间:2020-09-22 09:21 · 阅读:2077

react router 配置404页面

使用Vue相关的技术栈2年左右了,在路由管理上一直用的比较得心应手,现在的项目使用react开发,路由自然也就切换到了react router,所用版本为react router 4

在Vue router配置中,我们可以很简单的配置出404页面

使用通配符 * 号匹配所有路由,并将此配置放在数组的最末端,当前面的路由都匹配不上时,就会匹配到 * 号,然后我们使用redirect 配置将路由重定向到404component

此配置方法不管是对于单级路由还是嵌套路由都适用,只要匹配不到,最终都可以通过 * 来redirect

export default new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/404',
      name: 'home',
      component: Home
    },
    {
      path: '/about',
      name: 'about',
      component: () => import(/* webpackChunkName: "about" */ './views/About.vue'),
      children: [
        {
          path: 'page1',
          name: 'page1',
          component: page1
        },
        {
          path: 'page2',
          name: 'page2',
          component: page2
        },
      ]
    },
    {
      path: '*',
      redirect: '/404'
    }
  ]
})

那么 react router 4 有什么不同的地方呢?

首先是配置子路由的位置不同

react router 4配置嵌套路由是在所在组件里面进行配置

// Parent component
<Switch>
      <Route path="/Page" component={Page} />
      <Route path="/Sub" component={Childs} />
    </Switch>
// Child component
const Childs = ({ match }) => (
  <div>
    <Switch>
      <Route path={`/Sub/page1`} component={Page1} />
      <Route path={`/Sub/page2`} component={Page2} />
    </Switch>
  </div>
);

在父组件里定义好子组件

这样就可以通过 '/Sub/page1’访问到该路由页面了

其次,react router 4还在路由匹配上有些不同,R4没有通配符* ,取而代之的是在路由最后可以添加

<Switch>
  <Route path="/Page" component={Page} />
  <Route path="/Sub" component={Childs} />
  <Route component={NoMatch} />  // 匹配未找到的路由
</Switch>

这时候,我们添加404页面在根组件,it works!

但是,我们的需求往往不是这么简单,通常情况下,我们希望在所有路由情况下他都可以校验路由是否不存在

problem:

在嵌套路由下,我们发现:访问 /Sub/page3’’ 仍然可以看到不完整的路由页面,虽然没有匹配到page3,但是router匹配到了/Sub,这时候问题来了,子路由匹配不到404页面啊。

我查看了当前比较火的react前端解决方案 <react-admin>和<ant-design-pro>,发现他们都没有全局404页面的解决方案。

查了很久文档和他人的文章,最终找到一个解决方案,借鉴出处:一个神奇的网站

既然R4是在父组件内部配置路由的,那么内部也要添加404匹配路由

// 在子组件中添加404路由
 <Switch>
  <Route path={`/Sub/page1`} component={Page1} />
  <Route path={`/Sub/page2`} component={Page2} />
  <Route component={NoMatch} />
</Switch>
// NoMatch(404)组件
class NoMatch extends React.Component {
  componentDidMount() {
  }
  render() {
    return (
      <div className="404">
        404
      </div>
    );
  }
}

直接匹配404依然会渲染父组件,所以我们需要重定向

// RedirectAs404
const RedirectAs404 = ({ location }) => <Redirect to='/404' />
// 通过Redirect将当前路由重定向到
 <Switch>
  <Route path={`/Sub/page1`} component={Page1} />
  <Route path={`/Sub/page2`} component={Page2} />
  <Route component={RedirectAs404} />
</Switch>

发现直接redirect到404不太友好,页面路径也变成了404

在这里插入图片描述

在根组件添加一个state,来修改总路由配置

// RedirectAs404
const RedirectAs404 = ({ location }) => <Redirect to='{Object.assign({}, location, { state: { is404: true } })}' />
// App.js 这里指根组件
<Route render={({ location }) => (
  location.state && location.state.is404
    ? <NoMatch />
    : <Switch>
      <Route path="/Page" component={Page} />
<Route path="/Sub" component={Childs} />
        <Route component={RedirectAs404} />
    </Switch>
)} />

这样,不管是第几级的组件,当只要匹配到RedirectAs404组件,都会重定向到404页面,也就做到了全局404显示

更多相关技术文章,请访问HTML中文网

标签:
react

相关文章

react中怎么使用sass?

方法:1、使用npm工具安装sass-loader和node-sass依赖;2、配置webpack.config.js文件;3、在需要的组件中引入scss,然后添加scss代码即可。

react怎么判断checkbox是否被选中

react判断checkbox是否被选中的方法:可以通过【if ($(&quot;#checkbox-id&quot;).get(0).checked) {}】方法来进行判断。还可以通过原生js方法来进行判断,如【ele.checked】。

react子组件如何向父组件传值?

在react中,首先通过父组件给子组件传递一个函数,然后子组件通过参数传到父组件,通过props来传递函数的引用,并通过回调的方式实现;其实就是子组件调用父组件的方法,把值以形参的方式传出来。

react组件什么时候会重新渲染?

每当组件状态改变时,React就会调用render函数进行重新渲染。更改状态意味着当调用setState函数时,React触发更新;这不仅意味着将调用组件的render函数,而且还意味着重新渲染其所有后续子组件,无论其prop是否已更改。

react组件生命周期有几个阶段?

React组件生命周期有三个阶段:加载、更新和卸载。加载阶段表示一个组件实例被创建并被插入到DOM中;更新阶段表示由状态或属性的改变导致组件的重新渲染;卸载阶段表示组件从DOM中移除。

返回分类 返回首页