简体   繁体   English

React-Router:无法从/向/ home重定向

[英]React-Router : Unable to redirect from / to /home

I am using react-router for routing in my application. 我在我的应用程序中使用react-router进行路由。 Here is the router config. 这是路由器配置。

let routes = {
    path : "/",
    onEnter : ({},replace) => replace('/home'),
    childRoutes : [
        {
            path : "home",
            component : App,
            indexRoute : {component : Landing},
            childRoutes : [ ]
        }
    ]
}

When I try to load application I get RangeError: Maximum call stack size exceeded . 当我尝试加载应用程序时,我得到RangeError: Maximum call stack size exceeded As per my understanding, this is because of onEnter in "/" which run every time I try to hit anything. 根据我的理解,这是因为onEnter在“/”中,每当我尝试击中任何东西时都会运行。 Is there a way that it only executes when I hit exact match "/" and not every time ? 有没有一种方法只有当我达到完全匹配“/”而不是每次都执行时才会执行?

Use indexRoute to redirect to home route (see index redirects last example): 使用indexRoute重定向到home路由(请参阅索引重定向上一个示例):

let routes = {
    path : "/",
    indexRoute: { onEnter: (nextState, replace) => replace('/home') },
    childRoutes : [
        {
            path : "home",
            component : App,
            indexRoute : {component : Landing},
            childRoutes : [ ]
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM