反应路线:导航栏

时间:2019-11-11 06:49:43

标签: reactjs bootstrap-4 react-router

单击导航栏时,渲染组件时出现问题。因此,当我单击导航栏时,该组件将在导航栏内而不是在导航栏下方进行渲染。有什么我可以解决的吗?

这是App文件:

//App.js

import React from 'react';
import { BrowserRouter, Switch, Route } from 'react-router-dom';

import { Header, Content } from './component/Layout/index';

import Films from './container/Films/Films';
import Peoples from './container/Peoples/Peoples';

import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';

const App = () => {
  return (
    <BrowserRouter>
      <div className='App'>
        <Header />
        {/* <Content /> */}

        <Switch>
          <Route path='/films' component={Films} />
          <Route path='/peoples' component={Peoples} />
        </Switch>
      </div>
    </BrowserRouter>
  );
};

export default App;

这是导航栏文件:

//Header.js
import React from 'react';

import { Navbar, NavDropdown, Nav, NavItem } from 'react-bootstrap';

import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';

import Films from '../../container/Films/Films';
import Peoples from '../../container/Peoples/Peoples';

import classes from './Header.module.css';

const Header = () => {
  return (
    <Router>
      <div className={classes.header}>
        <Navbar bg='dark' expand='lg'>
          <Navbar.Brand href='#home' className='text-white'>
            Swapiku
          </Navbar.Brand>
          <Navbar.Toggle aria-controls='basic-navbar-nav' />

          <Nav className='mr-auto text-secondary'>
            <Nav.Link className='text-secondary' href='#films'>
              <Link to={'/films'}> Films </Link>
            </Nav.Link>
            <Nav.Link className='text-secondary' href='#people'>
              <Link to={'/peoples'}> People </Link>
            </Nav.Link>
          </Nav>
          <Switch>
            <Route path='/films' component={Films} />
            <Route path='/peoples' component={Peoples} />
          </Switch>
        </Navbar>
      </div>
    </Router>
  );
};

export default Header;

任何帮助将不胜感激,谢谢

3 个答案:

答案 0 :(得分:1)

因为导航内容位于导航栏中。 试试这个

   <div className={classes.header}>
    <Navbar bg='dark' expand='lg'>
      <Navbar.Brand href='#home' className='text-white'>
        Swapiku
      </Navbar.Brand>
      <Navbar.Toggle aria-controls='basic-navbar-nav' />

      <Nav className='mr-auto text-secondary'>
        <Nav.Link className='text-secondary' href='#films'>
          <Link to={'/films'}> Films </Link>
        </Nav.Link>
        <Nav.Link className='text-secondary' href='#people'>
          <Link to={'/peoples'}> People </Link>
        </Nav.Link>
      </Nav>
    </Navbar>
   <Router>
      <Switch>
        <Route path='/films' component={Films} />
        <Route path='/peoples' component={Peoples} />
    </Switch>
  </Router>
  </div>

答案 1 :(得分:0)

如下所示从Header.js删除开关


import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';

import Films from '../../container/Films/Films';
import Peoples from '../../container/Peoples/Peoples';

import classes from './Header.module.css';

const Header = () => {
  return (
    <Router>
      <div className={classes.header}>
        <Navbar bg='dark' expand='lg'>
          <Navbar.Brand href='#home' className='text-white'>
            Swapiku
          </Navbar.Brand>
          <Navbar.Toggle aria-controls='basic-navbar-nav' />

          <Nav className='mr-auto text-secondary'>
            <Nav.Link className='text-secondary' href='#films'>
              <Link to={'/films'}> Films </Link>
            </Nav.Link>
            <Nav.Link className='text-secondary' href='#people'>
              <Link to={'/peoples'}> People </Link>
            </Nav.Link>
          </Nav>
        </Navbar>
      </div>
    </Router>
  );
};

export default Header;

答案 2 :(得分:0)

您也在标题组件中使用了相同的路由代码。 从Header.js删除路由器代码

请更新Header.js

//Header.js
import React from 'react';

import { Navbar, NavDropdown, Nav, NavItem } from 'react-bootstrap';

import { Link } from 'react-router-dom';

import Films from '../../container/Films/Films';
import Peoples from '../../container/Peoples/Peoples';

import classes from './Header.module.css';

const Header = () => {
  return (

      <div className={classes.header}>
        <Navbar bg='dark' expand='lg'>
          <Navbar.Brand href='#home' className='text-white'>
            Swapiku
          </Navbar.Brand>
          <Navbar.Toggle aria-controls='basic-navbar-nav' />

          <Nav className='mr-auto text-secondary'>
            <Nav.Link className='text-secondary' href='#films'>
              <Link to={'/films'}> Films </Link>
            </Nav.Link>
            <Nav.Link className='text-secondary' href='#people'>
              <Link to={'/peoples'}> People </Link>
            </Nav.Link>
          </Nav>
        </Navbar>
      </div>

  );
};

export default Header