从反应路由器 dom 中的基本 url 中删除尾部斜杠

问题描述

我有一个使用 react-router-dom 的 React 项目。我实现了 browserrouter there

所有网址必须类似于 http://localhost:8080/build/index.html?path=qmrelsr3pk2nnubhmb6iobj6ynr3ahiehuqa4

我在 browserRouter 中的 basename/build/index.html

在 DOM 中生成的所有链接都具有正确的结构。但是,如果我单击其中之一,它会在 index.html 后面添加一个尾随 /。所以如果没有正确配置的服务器,我无法刷新页面

如何防止添加 /

亲切的问候。

解决方法

我像这样入侵了 history.push

history.pushState = ((f) => function pushState(){
  arguments[2] = arguments[2].replace('index.html/','index.html');
  const ret = f.apply(this,arguments);

  return ret;
})(history.pushState);

我确定这不是最佳做法,但它确实有效。

,

我就是这样做的:

<Redirect
    from='/:url*(/+)'
    to={window.location.pathname.slice(0,-1)}
/>

<Router><Switch> 组件的开头。它检查是否有尾部斜杠并重定向到没有尾部斜杠的页面。

我也不确定最佳实践并尊重@pgalle 的回答