【问题标题】:React routing error in sub directory反应子目录中的路由错误
【发布时间】:2017-05-15 23:03:58
【问题描述】:

我做了路由

    render(
      <Provider store={store}>
          <Router history={browserHistory }>
            <Route path='/MyAppName' component={TopContainer}>
                <IndexRoute component={Login} />
                <Route path='/main' component={MainRouteContainer} />   
                <Route path='/popup' component={StackViewContainer} />  
            </Route>
        </Router>
      </Provider>,
      document.getElementById('root')
    )

我有一个 apache http 目录

/var/www/html

因为还有其他网络应用所以我需要一个子目录

/var/www/html/MyAppName

并且上面目录中的文件是index.html和bundle.js。

如果我使用上面的路由代码访问它,它可以正常工作

http://www...com/MyAppName

问题是“/main”一旦被访问,地址字段中的url就变成了 http://www...com/MyAppName/main

此时,页面重新加载会导致“找不到页面”,因为 MyAppName 下没有“main”(只有 bundle.js)。

有什么建议吗?

谢谢

【问题讨论】:

    标签: reactjs routes


    【解决方案1】:

    您可以将basename 属性添加到Router 标记

    <Router history={browserHistory} basename="/subdirectory/path">
    

    或在react-router-dom 中使用HashRouter。有了 hashRouter,就不需要basename attribute

    <HashRouter>
    

    【讨论】:

      猜你喜欢
      • 2020-04-15
      • 2021-05-06
      • 2016-10-24
      • 2021-03-01
      • 2020-12-05
      • 2016-01-04
      • 2016-03-07
      • 1970-01-01
      相关资源
      最近更新 更多