【问题标题】:routing is not after making build in react js web application路由不是在构建 React js Web 应用程序之后
【发布时间】:2018-09-06 19:33:16
【问题描述】:

我在 React js 中创建了一个应用程序。在开发模式下一切正常,但在构建路由后无法正常工作。

我的 package.json 文件代码

{ "homepage": "http://localhost/hositng/react/",
    "private": true,
    "entry": {
         "index": "./index.js"
     },
}

我的路线文件代码

const Routes = () => (
<BrowserRouter basename="http://localhost/hositng/react" >
    <Switch>
        <Route exact path='/' component={Login} />
        <Route path='/login' component={Login} />
        <Route path='/home' component={Home} />
        <Route path='/contact' component={Contact} />
    </Switch>
</BrowserRouter>);

react build 命令后,当我点击“http://localhost/hositng/react/”url 时

  1. 应该进入登录页面,但进入主页
  2. 当点击联系我们页面链接时,联系页面 url 正在发生变化。

    它应该转到这个网址“http://localhost/hositng/react/contact” 但 它将转到“http://localhost/contact”这个网址

但是当我用这个 url 刷新浏览器“http://localhost/contact”时,空白页来了

【问题讨论】:

    标签: reactjs react-redux react-router


    【解决方案1】:

    在 Switch 中定义的路由看起来不错。我已经使用它并且它有效。 我会建议在 basename 属性中使用相对路径而不是绝对路径。

    <BrowserRouter basename="/hositng/react" >
    

    欲知更多详情BrowserRouter doc

    【讨论】:

    • 我已经尝试过这个,但它不适合我。注意:通常它对我有用,但我正在使用 npm run build 命令创建优化的构建,之后我遇到了编译器代码的这个问题。我在 xammp 下的 localhost 中运行此编译器代码的另一件事。有什么问题吗?
    猜你喜欢
    • 2017-09-05
    • 1970-01-01
    • 2022-01-08
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多