反应路由器不更改路径

时间:2019-12-22 10:16:02

标签: reactjs react-router

import * as React from "react";
import { BrowserRouter as Router,Switch,Route,Link,useHistory } from "react-router-dom";
import Login from "./login/index";
import MainFrame from "./main-frame/index";

const MainWrapper = () => {

  return (
    <div>
      <Router>      
      <Switch>
          <Route path="/main-frame">
          <MainFrame  /> 
          </Route>
          <Route exact path="/">
          <Login />
          </Route> 
          <Route exact path="/i">
          <p>sdf</p>
          </Route>       
        </Switch>
      </Router> 
    </div>
  );
};

export default MainWrapper;

这是我的简单代码。当我浏览并手动输入 http://localhost:8080/i 时,路径未更改。仅 http://localhost:8080/ 有效。 http://localhost:8080/main-frame 均无效。 上面两个都给出错误:

Cannot GET /i
Cannot GET /main-frame

1 个答案:

答案 0 :(得分:0)

我认为您必须修复它

<Router>      
  <Switch>
    <Route path="/main-frame" component={MainFrame} />
    <Route exact path="/" component={Login} />
    {/* <Route exact path="/i"> */}
  </Switch>
</Router> 

参考此https://codeburst.io/getting-started-with-react-router-5c978f70df91