我是 React 的新手,正在使用 MDB(Bootstrap 的材料设计)我从这个站点获取了 NavBar,并将链接更改为我自己的路线。该站点的地址正在更改,但是该站点永远不会更改为所需页面。我必须告诉你,我已经使用过几次 React Router 并且从来没有遇到过这样的事情。主要问题和 CRAZY S*** 是我有另一个可以正常工作的链接标签,但只是在 NAVBAR 中存在问题。
索引.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router} from "react-router-dom";
import '@fortawesome/fontawesome-free/css/all.min.css';
import 'bootstrap-css-only/css/bootstrap.min.css';
import 'mdbreact/dist/css/mdb.css';
import App from './App';
ReactDOM.render(
<Router>
<App/>
</Router>
,document.getElementById('root'));
应用程序.js
import React from 'react';
import { BrowserRouter as Router, Route,Switch} from "react-router-dom";
import { Link } from "react-router-dom";
import Home from './pages/Home';
import Courses from './pages/Courses'
import Navbar from './components/Navbar'
import Signin from './pages/Sign-in'
import Register from './pages/Register'
import ErrorPage from './components/ErrorPage'
import SingleCourse from './pages/SingleCourse'
import './App.css'
import {
MDBContainer,
} from 'mdbreact';
import Footer from './components/Footer'
function App() {
return (
<>
<Navbar/>
<MDBContainer>
<Switch>
<Route exact path="/" component={Home}/>
<Route exact path="/courses" component={Courses}/>
<Route exact path="/courses/:id" component={SingleCourse}/>
<Route path="/sign-in" component={Signin}/>
<Route path="/register" component={Register}/>
<Route component={ErrorPage}/>
</Switch>
</MDBContainer>
<Footer/>
</>
);
}
export default App;
导航栏
import React, { Component } from "react";
import { Link,BrowserRouter as Router } from "react-router-dom";
import Home from '../pages/Home'
import {
MDBNavbar, MDBNavbarBrand, MDBNavbarNav, MDBNavItem, MDBNavLink, MDBNavbarToggler, MDBCollapse, MDBFormInline,
MDBDropdown, MDBDropdownToggle, MDBDropdownMenu, MDBDropdownItem
} from "mdbreact";
class NavbarPage extends Component {
state = {
isOpen: false
};
toggleCollapse = () => {
this.setState({ isOpen: !this.state.isOpen });
}
handleClick() {
;
}
render() {
return (
<Router>
<MDBNavbar className="Navbar" dark expand="md" style={{backgroundImage: "linear-gradient(" + "to left, #D53D96 , #7A2A90"+" )"}}>
<MDBNavbarBrand>
<strong className="white-text">Navbar</strong>
</MDBNavbarBrand>
<MDBNavbarToggler onClick={this.toggleCollapse} />
<MDBCollapse id="navbarCollapse3" isOpen={this.state.isOpen} navbar>
<MDBNavbarNav left>
<MDBNavItem >
<MDBNavLink to="/"> ////////HERE iS THE PROBLEM(I Change It Many Times!)
Home
</MDBNavLink>
</MDBNavItem>
<MDBNavItem>
<MDBDropdown>
<MDBDropdownToggle nav caret>
<span className="mr-2">Courses</span>
</MDBDropdownToggle>
<MDBDropdownMenu>
<MDBDropdownItem href="#!">Action</MDBDropdownItem>
<MDBDropdownItem href="#!">Another Action</MDBDropdownItem>
<MDBDropdownItem href="#!">Something else here</MDBDropdownItem>
<MDBDropdownItem href="#!">Something else here</MDBDropdownItem>
</MDBDropdownMenu>
</MDBDropdown>
</MDBNavItem>
<MDBNavItem>
<MDBFormInline waves>
<div className="md-form my-0">
<input className="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search" />
</div>
</MDBFormInline>
</MDBNavItem>
</MDBNavbarNav>
<MDBNavbarNav right>
<MDBNavItem>
<MDBNavLink to="#!">My Courses</MDBNavLink>
</MDBNavItem>
<MDBNavItem>
<MDBNavLink to="#!">Personal Info</MDBNavLink>
</MDBNavItem>
</MDBNavbarNav>
</MDBCollapse>
</MDBNavbar>
</Router>
);
}
}
export default NavbarPage;