更新时间:2023-11-22 18:44:52
这对我有用:
<Router basename={window.location.pathname.replace(/(\/[^/]*)$/, "")}>
这没有文档中要求的尾部斜杠:https://reactrouter.com/web/api/BrowserRouter/basename-string
this has no trailing slash as required in docs: https://reactrouter.com/web/api/BrowserRouter/basename-string
示例
var pathname = "/pathname/routeName"
console.log(pathname.replace(/(\/[^/]*)$/, "") //returns "/pathname"