我可以让react路由器只覆盖一些url,并将其余的url正常传递给服务器吗

Can I make react-router cover only some urls and pass rest of the urls normally to the server?

本文关键字:url 常传递 服务器 路由器 react 覆盖 我可以      更新时间:2023-09-26

我为react-router定义了一些路由。我不希望所有其他路由都像没有react-router一样正常地通过服务器。

如果我只是不指定这些路由,当页面加载时,我会收到一个警告:"警告:[反应路由器]位置"/Services"与任何路由都不匹配",但我可能会遇到这种情况。

我不能忍受的是,当我在react路由器覆盖的url上,点击返回按钮返回到未覆盖的url时,它就不起作用了。我只是再次看到这个错误,服务器并没有再次被要求提供未覆盖的url。

知道如何让网站只被react路由器部分覆盖吗?

实现这一点的一种方法是手动使用react-router,就像在服务器端一样。

# this is CoffeScript (cjsx)
window.router =
  <Router history={browserHistory}>
    <Route name="ServiceNotes" path="ServiceNotes/:location_id" component="ServiceNote" />
  </Router>
$(document).ready ->
  routes = createRoutes window.router.props.children
  initialLoad = true
  browserHistory.listen (location) ->
    match { routes, location: location.pathname }, (error, redirectLocation, renderProps) ->
      if !error && renderProps
        render <RouterContext {...renderProps} />, document.getElementById('page-container')
      else
        console.log "Server side navigation"
        if !initialLoad
          window.location.reload()
    initialLoad = false
  1. 听听历史
  2. 匹配路线
  3. 如果路由匹配,请像在服务器端一样进行渲染
  4. 如果没有路由匹配,在初始历史事件上什么也不做(我们刚刚来到这里),但在后续事件上从服务器重新加载当前url(历史阻止了在点击页面上的后退按钮或链接后调用服务器,但我们需要它通过,因为它是不匹配的路由,所以我们手动执行)