【问题标题】:browserHistory: React router not finding pathbrowserHistory:反应路由器找不到路径
【发布时间】:2025-11-24 14:20:05
【问题描述】:

我正在尝试使用 browserHistory 并向路径添加参数,但是在向路径添加参数时我无法找到页面,因为浏览器正在查看的路径不正确,因为它在前面添加了“AddAmendLeague”的网址。

(http://localhost:8080/Home) 路径应该是什么:

(http://localhost:8080/AddAmendLeague/1) 现在浏览器找不到我的client.js:

ReactDOM.render(<Router history={browserHistory }>
                <Route path='*' component={Layout}>
                    <IndexRoute         component={Home}/>
                    <Route path="LeagueList" component={LeagueList}/> 
                    <Route path="/AddAmendLeague/:LeagueID" component={AddAmendLeague}/>
                </Route>

            </Router>, app);

index.html

<script src="client.min.js"></script>

关于我做错了什么有什么想法吗?对不起,如果这是一个愚蠢的错误。

不太确定你是否需要我的 package.JSON

包.JSON

{
  "name": "bableandtypescript",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack-dev-server --content-base src --inline --hot --watch --history-api-fallback"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.5",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-react-html-attrs": "^2.0.0",
    "babel-plugin-transform-class-properties": "^6.16.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-stage-0": "^6.16.0",
    "debug": "^2.3.2",
    "es6-promise": "^4.0.5",
    "flux": "^3.0.0",
    "history": "^4.3.0",
    "json-loader": "^0.5.4",
    "mysql": "^2.12.0",
    "net": "^1.0.2",
    "radium": "^0.18.1",
    "react": "^15.3.2",
    "react-dom": "^15.3.2",
    "react-router": "^2.8.1",
    "undebug": "^1.0.0",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.16.2",
    "websocket": "^1.0.23"
  }
}

【问题讨论】:

    标签: reactjs react-router browser-history


    【解决方案1】:

    我不确定我是否正确理解了您的问题。但也许你应该使用 hashhistory 而不是 browserhistory 来路由 https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md#hashhistory

    然后您可以使用带有井号 # 标记的 url (http://localhost:8080/#/AddAmendLeague/1)

    如果你想要漂亮的 url '/' 和 browserHistory 你还应该配置你的服务器来处理这个 url https://github.com/ReactTraining/react-router/blob/master/docs/guides/Histories.md#configuring-your-server

    【讨论】:

    • 嗨,hashHistory 确实有效,但我正在努力保持 URL 的美观,而 browserhistory 提供了这一点。 browserhistory 路由的工作方式是否与 hashHistory 不同?
    • 根据您的问题,您似乎不清楚这行代码。 这行表示您将转到 AddAmendLeague 并有一个名为 LeagueID 的参数。我不明白你的问题吗?
    • 如果你想要带有 browserHistory 的漂亮 url,你还应该配置你的服务器 (github.com/ReactTraining/react-router/blob/master/docs/guides/…)