2017-06-04 133 views
3

我遇到了一個問題,即使用react-router-dom的路由一次顯示所有路由。所以當我渲染我的應用程序組件和包含路由的開始時,我會看到每個路由的頁面在彼此之上。所以它看起來像這樣:反應路由器v4一次顯示多條路線

enter image description here

的sooo開始這是怎麼踢了,這是我的index.jsx被顯示在網頁上的應用程序組件:

// Dependencies 
import React from 'react'; 
import ReactDOM from 'react-dom'; 

// Components 
import App from './components/app.jsx'; 

// Styles 
import './index.scss'; 

const container = document.getElementById('main'); 

ReactDOM.render(<App />, container); 

在我應用程序組件,路由設置爲這樣:

// Dependencies 
import React from 'react'; 
import { BrowserRouter as Router, Route, Match, Miss } from 'react-router-dom'; 

// Components 
import Login from './pages/login/login.jsx'; 
import Home from './pages/home/home.jsx'; 

// Styles 
import './app.scss'; 

const App =() => { 
    return (
    <Router> 
     <div> 
      <Route exact pattern='/' component={Login}/> 
      <Route exact pattern='/home' component={Home}/> 
     </div> 
    </Router> 
); 
}; 

export default App; 

這是我的登錄/主頁組件(歸屬組件是完全一樣的只是更換登錄):

// Dependencies 
import React from 'react'; 

// Styles 
import './login.scss'; 

class Login extends React.Component { 
    render() { 
    return (
     <div className="content"> 
     <div className='title'> 
      Login Page 
     </div> 
     </div> 
    ); 
    } 
} 

export default Login; 

我還注意到,當我瀏覽到http://localhost:9999/home我也收到此錯誤:

enter image description here

東西顯然接線不正確,我有點糊塗了通過與不同的文檔現有版本。有關如何得到這個固定的建議非常感謝!

+2

您是否嘗試過使用'path'而不是'pattern'? – Li357

+0

雖然停止了每頁的呈現,但仍然存在問題,但是當我導航到/ home時,主頁組件未加載 – Shawn

+1

您必須告訴服務器始終爲索引提供服務,以便您的反應應用程序可以處理路由。 – xDreamCoding

回答

3

React Training

路線不帶路徑總是匹配。

所以在你的情況下,因爲你在使用模式,所以你的情況都會一直匹配。

+0

像@Andrew Li提到的那樣,它確實停止了渲染 – Shawn