我想知道是否可以在路由配置对象中使用"indexRedirect"(因此不使用JSX)。
我尝试但没有成功,所以我认为目前不支持此功能,但随后我的问题 (https://github.com/reactjs/react-router/issues/3150) 在没有评论的情况下关闭,所以我仍然不知道这是因为该功能已经存在但我滥用了它,还是因为这是一个不需要的功能。
提前谢谢。 :)
如文档中所述,相当于使用普通配置对象的 indexRedirect 如下所示
const routes = [{
path: '/',
component: App,
indexRoute: { onEnter: (nextState, replace) => replace('/welcome') },
childRoutes: [
{ path: 'welcome', component: Welcome },
{ path: 'about', component: About }
]
}]
使用普通路由配置,它是一个更底层的 API,并且在 routes 对象中没有特殊的redirect
或indexRedirect
键。相反,您可以使用onEnter
来完成重定向。方法如下:
import React from 'react'
import { render } from 'react-dom';
import { Router, browserHistory } from 'react-router'
const Home = () => <h3>Home</h3>
const About = () => <h3>About</h3>
const routes = [{
path: '/',
component: Home,
onEnter: (nextState, replace) => replace('/about')
}, {
path: '/about',
component: About
}]
render(
<Router history={browserHistory} routes={routes} />,
document.getElementById('root')
);
编辑:将其也添加到文档中。
我会欣赏与 JGX 相同的事情:为这种重定向工作相对路径