且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

部署在不同位置(子目录)的 React-router 应用程序

更新时间: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" 

参见:https://regexr.com/5v3s3