为什么在react历史上createHashHistory为每个路径添加#?

问题描述 投票:0回答:1

我有一个应用程序的历史配置是这样的。

import { createHashHistory } from 'history';
import { ConnectedRouter } from 'connected-react-router';

const history = createHashHistory({
  hashType: 'slash',
});
    ...
  <ConnectedRouter history={history}>
    <App />
  </ConnectedRouter>

但我所有的路径都被 /#localhost:8080/ 变成。localhost:8080/#/

我已经尝试更新我的软件包为 这个问题说 但它没有工作。

唯一有效的是改变 createHashHistorycreateBrowserHistory但我不知道它们之间有什么区别,为什么?createHashHistory 是附加的 /#

javascript reactjs react-router react-router-dom history.js
1个回答
1
投票

使用hashHistory,它产生的网址是这样的http:/yourwebsite.net#pagexxx。

使用浏览器历史记录,它产生的网址是http:/yourwebsite.netpagexxx。

该用哪个?在现实世界的产品中,大多使用BrowserHistory。一个经验法则是:"如果你使用的是一个可以处理动态URL的动态服务器,那么你需要使用BrowserRouter组件,但如果你使用的是一个只为静态文件服务的服务器,那么在这种情况下应该使用HashRouter组件。"

在你的代码中,hashType: '斜线'只是默认值。

© www.soinside.com 2019 - 2024. All rights reserved.