【发布时间】:2016-06-02 07:06:55
【问题描述】:
我已经用这个(react-router 2.0)在路由器上设置了 browserHistory:
import { browserHistory } from 'react-router'
function requireAuth(nextState, replace) {
if (!services.auth.loggedIn()) {
replace({
pathname: '/login',
state: { nextPathname: nextState.location.pathname }
})
}
}
export default (store) => (
<Router history={browserHistory}>
<Route path='/' component={AppLayout}>
<Route path="login" component={LoginContainer} />
<Route path="map" component={MapContainer} onEnter={requireAuth} />
</Route>
</Router>
);
然后我尝试在 react-router 中使用 browserHistory 以编程方式从视图路由到新页面,ala:
import { browserHistory } from 'react-router'
...
browserHistory.push('/map');
这会将 URL 更改为 /map,但不会呈现该路由中的组件。我做错了什么?
【问题讨论】:
-
我可以看看你的
requireAuth处理程序,还有地图视图吗? -
好的 - 已添加。注意:如果我没有 onEnter 属性,也会发生同样的事情(/map 未呈现)。
-
是的 - 调用了 browserHistory.push('/map') - 我看到 URL 发生了变化,但是新的 Route 组件 (MapContainer) 没有呈现。最后,为了完整起见,如果我关闭 auth,然后直接转到 /map,它会正确呈现。
-
您是否在此应用程序后面使用服务器? Nginx,还是别的什么?这些需要设置为使用全部路由并将所有内容发送到 index.html。我相信你可能知道。因为你有使用 ember 的经验。
-
是的 - 我特别使用这个应用程序种子:github.com/davezuko/react-redux-starter-kit
标签: reactjs react-router