简体   繁体   English

ReactRouter 的哈希路由器无法处理哈希更改

[英]ReactRouter's Hash Router not working on hash change

I'm using ReactRouter's <HashRouter> class (my server isn't configured for <BrowserRouter> and this is an internal tool so I don't care about search engines or etc).我正在使用 ReactRouter 的<HashRouter>类(我的服务器没有为<BrowserRouter>配置,这是一个内部工具,所以我不关心搜索引擎等)。

I have precisely one route:我只有一条路线:

<Route path="/:datestring" component={ConnectedDatePage} />

On my development version, this all works perfectly.在我的开发版本上,这一切都完美无缺。 I then package the app up (using webpack) into a single page and publish it, and on the server, clicking links changes the hash—but there is no corresponding change on the page.然后我将应用程序(使用 webpack)打包成一个页面并发布它,在服务器上,单击链接会更改哈希——但页面上没有相应的更改。

Reloading the page with the new hash renders everything correctly, but then changing the hash again does nothing.使用新的哈希重新加载页面可以正确呈现所有内容,但再次更改哈希没有任何作用。 What might be going on here?这里可能会发生什么?

UPDATE: I suspect it may be because, on the server, it is not running at the root level (it's at, eg, http://example.com/app/#/20170203/ instead of http://localhost/#/20170203/ ).更新:我怀疑这可能是因为在服务器上,它没有在根级别运行(例如,它位于http://example.com/app/#/20170203/而不是http://localhost/#/20170203/ )。 I don't know how to configure the router to handle this, though.不过,我不知道如何配置路由器来处理这个问题。

My issue was I was not specifying exact option in the routes:我的问题是我没有在路线中指定exact选项:

<Route exact path="/" component={Home} />
<Route exact path="/:datestring" component={ConnectedDatePage} />

Without this option, Home page was being rendered at all times.如果没有此选项,主页将始终呈现。 Very basic but fatal mistake.非常基本但致命的错误。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM