React路由器仅适用于/ path

时间:2017-04-03 11:49:55

标签: reactjs webpack ecmascript-6 react-router

  

为什么只使用root路径,当我到localhost时:8090 /关于它   说的。无法获得localhost:8090 / about

import React,{Component} from "react"
    import {BrowserRouter as Router,Route} from "react-router-dom"

var About =()=>{
    return <div>About us{}</div>
}

class App extends Component{
render(){
return (
    <Router>
 <div>
      <Route path ="/" component={About}/>
      <Route path ='/about'component={About}/>
</div>
    </Router>
);
}
}
export default App;
  

main.js入口点(webpack)

import React from "react"
import ReactDOM from "react-dom"
import App from "./components/App.jsx"

ReactDOM.render(

       <App />,
      document.getElementById('table')
);
  

我使用“react-router-dom”:“^ 4.0.0-beta.7”

1 个答案:

答案 0 :(得分:0)

在您的组件中添加Link,以更改路线,将其写为:

import {BrowserRouter as Router,Route, Link} from "react-router-dom"

class App extends Component{
  render(){
    return (
      <Router>
        <div>
           <Link to='/about'>About </Link>
           <Route path ="/" component={About}/>
           <Route path ='/about'component={About}/>
        </div>
      </Router>
    );
  }
}
export default App;