大家好,我是reactjs的新手,我正在尝试学习reactjs路由器。我的仪表板路线上发生这种奇怪的事情。 我的App组件有两条路由“ / login”和“ /”
'/ login'将显示登录屏幕,而'/'将显示主仪表板屏幕。 在仪表板上,我具有侧面导航组件和主要内容组件。 我正在尝试在主要组件内嵌套一组Routes。 当我通过侧面导航组件中的链接导航时,路线工作正常。但是,当我尝试在浏览器中单击“后退”或“前进”按钮时,它不起作用。我的屏幕空白,没有任何内容。
请需要您的帮助。.任何帮助将不胜感激。
请在下面找到代码。
//App.js
import React, { Component } from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import MainPage from "./Mainpage";
import LoginPage from "./components/pages/LoginPage";
import "./styleFiles/index.css";
class App extends Component {
render() {
return (
<div className="flexible-content">
<Route path="/" exact component={MainPage} />
<Route path="/login" component={LoginPage} />
</div>
);
}
}
export default App;
//MainPage.js
import React, { Component } from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import SideNavigation from "./components/sideNavigation";
import Footer from "./components/Footer";
import "./styleFiles/index.css";
import DashboardPage from "./components/pages/DashboardPage";
import ProfilePage from "./components/pages/ProfilePage";
import TablesPage from "./components/pages/TablesPage";
import InventoryPage from "./components/pages/InventoryPage";
class MainPage extends Component {
render() {
return (
<BrowserRouter>
<div className="flexible-content">
<SideNavigation />
<main id="content" className="p-5">
<Switch>
<Route path={"/"} exact component={DashboardPage} />
<Route path={"/Dashboard"} exact component={DashboardPage} />
<Route path="/profile" component={ProfilePage} />
<Route path="/tables" component={TablesPage} />
<Route path="/inventory" component={InventoryPage} />
<Route component={DashboardPage} />
</Switch>
</main>
<Footer />
</div>
</BrowserRouter>
);
}
}
export default MainPage;
//sideNavigation.js
import React from "react";
import logo from "../assets/logo_.svg";
import { MDBListGroup, MDBListGroupItem, MDBIcon } from "mdbreact";
import { NavLink } from "react-router-dom";
import "../styleFiles/sideBar.css";
const sideNavigation = () => {
return (
<div className="sidebar-fixed position-fixed">
<a href="#!" className="logo-wrapper waves-effect">
<img alt="MDB React Logo" className="img-fluid" src={logo} />
</a>
<MDBListGroup className="list-group-flush">
<NavLink exact={true} to="/" activeClassName="activeClass">
<MDBListGroupItem className="bgc">
<MDBIcon icon="chart-pie" className="mr-3" />
Testing Dashboard
</MDBListGroupItem>
</NavLink>
<NavLink to="/profile" activeClassName="activeClass">
<MDBListGroupItem className="bgc">
<MDBIcon icon="user" className="mr-3" />
Projects
</MDBListGroupItem>
</NavLink>
<NavLink to="/tables" activeClassName="activeClass">
<MDBListGroupItem className="bgc">
<MDBIcon icon="table" className="mr-3" />
Tables
</MDBListGroupItem>
</NavLink>
<NavLink to="/inventory" activeClassName="activeClass">
<MDBListGroupItem className="bgc">
<MDBIcon icon="fas fa-truck-loading" className="mr-3" />
Inventory
</MDBListGroupItem>
</NavLink>
<NavLink to="/404" activeClassName="activeClass" />
</MDBListGroup>
</div>
);
};
export default sideNavigation;
在此先感谢您的帮助……
答案 0 :(得分:0)
当我对代码进行少量更改时,我可以消除这种怪异的行为。 更改仅对我的App.js文件进行。 请在下面找到更改
import React, { Component } from "react";
import { BrowserRouter, Route, Switch } from "react-router-dom";
import MainPage from "./Mainpage";
import LoginPage from "./components/pages/LoginPage";
import "./styleFiles/index.css";
class App extends Component {
render() {
return (
<div className="flexible-content ">
<Switch>
<Route path="/Main" exact component={MainPage} />
<Route path="/login" component={LoginPage} />
<Route component={MainPage} />
</Switch>
</div>
);
}
}
export default App;
我在我的app.js文件中添加了一个switch标签和一个Generic Not Found路由组件。