2017-09-24 94 views
0

我遇到了使用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> 

回答

0

它沒有工作,是我試圖從index.html的像src="./bundle.js"造成訪問bundle.js原因當我手動鍵入任何嵌套url時,客戶端從'/userId/bundle.js'中取得它。當我將這個包的路徑硬編碼到index.html,像http://localhost:8080/bundle.js那樣修復了這個問題。