问答

react-router重定向设置失效的问题?

作者:admin 2021-05-12 我要评论

项目中按照以下路由设置,但是重定向无效,是何原因? router.js const routes = [ { path: '/', onEnter: requireAuth, component: require('./components/Main...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)
项目中按照以下路由设置,但是重定向无效,是何原因?

router.js

const routes = [
    {
        path: '/',
        onEnter: requireAuth,
        component: require('./components/MainLayout/Layout').default,
        indexRoute: { component: require('./routes/PublicLibrary/components/PublicLibrary')},
        childRoutes: [
            require('./routes/PublicLibrary'),//公共库
            require('./routes/WorkTable'),//工作台
        ],
    },
    require('./routes/EditProject'),//工作台-编辑
    require('./routes/Login')//登录
];
const App = () => (
    <Provider {...stores}>
        <Router history={hashHistory} routes={routes}>
            <Redirect from="/" to="/publiclibrary"/>
        </Router>
    </Provider>
);
export default hot(module)(App);

地址栏中输入项目地址:localhost:8585,并未重定向至localhost:8585/#/publiclibrary

###

在Redirect中加上exact

<Redirect from="/" exact to="/publiclibrary"/>

###

当前的解决办法:

在项目Layout组件将要挂载的时候,监听props的children是否存在,不存在则设定默认路由。

componentWillMount() {
   if(!this.props.children)
      hashHistory.push('/publiclibrary')
}

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • react-router重定向设置失效的问题?

    react-router重定向设置失效的问题?

  • sed 命令删除指定行

    sed 命令删除指定行

  • iconfont图标加载不出来。只显示了一个

    iconfont图标加载不出来。只显示了一个

  • spring boot上传文件时数据流二次读取

    spring boot上传文件时数据流二次读取

腾讯云代理商
海外云服务器