当路径输入到浏览器的地址栏时,react-router v4无法呈现嵌套路由

问题描述:

我遇到了使用react-router v4呈现嵌套路由的问题。每当我在浏览器的地址栏中输入bla-bla.com/userId/postId时,都不会呈现任何东西,但是当我转到bla-bla.com/userId后,单击某个帖子的链接并将其重定向到帖子的路由时,所有内容工作正常,我看到帖子的页面。当我手动键入'/:userId /:blogId'时,我在bundle.js中看到了我的index.html文件的内容,这导致了错误。我可以做什么来使'/:userId /:blogId'路线无需首先进入'/:userId'即可访问?这就是我如何使其在服务器上:当路径输入到浏览器的地址栏时,react-router v4无法呈现嵌套路由

app.use('*',(req,res) => res.sendFile(path.resolve(__dirname, '..', 'public', 'index.html'))); 

路由配置:

<Switch> 
     <PrivateRoute component={Users} exact={true} path={'/users'}/> 
     <PrivateRoute component={BlogPage} exact={true} path={'/:userId/:blogId'}/> 
     <PrivateRoute component={UserPage} exact={true} path={'/:userId'}/> 
    </Switch> 

它没有工作,是我试图从index.html的像src="./bundle.js"造成访问bundle.js原因当我手动键入任何嵌套url时,客户端从'/userId/bundle.js'中取得它。当我将这个包的路径硬编码到index.html,像http://localhost:8080/bundle.js那样修复了这个问题。