首页工作时,路由在我的代码中无法执行“ / about”

时间:2019-12-30 06:53:17

标签: reactjs routing

我的代码中显示错误Cannot GET /about的路由不起作用,我的代码如下:

import React from "react";
import ReactDOM from "react-dom";
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import Home from "./Components/Home";
import About from "./Components/About";

const App = () => (
  <Router>
    <Switch>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
    </Switch>
  </Router>
);

ReactDOM.render(<App />, document.getElementById("root"));

2 个答案:

答案 0 :(得分:2)

确切定义您的路线-<Route exact path="/" component={Home} />

             <Router>
                <Switch>
                  <Route exact path="/" component={Home} />
                  <Route path="/about" component={About} />
                </Switch>
            </Router>

完整代码

const App = () => (
  <Router>
      <Switch>
          <Route exact path="/" component={Home} />
          <Route path="/about" component={About} />
      </Switch>
  </Router>
)
const Home = ()=>(<div>
<h3>Home</h3>
<Link to="/about">Visit to about page</Link>
</div>)
const About = ()=><h3>About</h3>

答案 1 :(得分:0)

如何使用到达/路由器? 由同一作者ryan florence开发

它通过更好的路由匹配算法和易用性解决了确切的路径问题。

相同的工作示例。 https://codesandbox.io/s/sad-chebyshev-gkw31