Bootstrap导航栏崩溃不起作用

Bootstrap导航栏崩溃不起作用

本文介绍了React Bootstrap导航栏崩溃不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了react bootstrap导航栏,还使用了 react-scroll 进行平滑导航。它工作正常,但是在响应模式下单击任何导航项时,导航栏不会折叠。

I have used react bootstrap navbar also used react-scroll for smooth navigation. It's working fine but navbar is not collapsing when clicking any nav item in the responsive mode.

包装

import React, { Component } from "react";
import { NavLink } from "react-router-dom";
import { Link } from "react-scroll";
import { LinkContainer } from "react-router-bootstrap";
import { Navbar, Container, NavDropdown, Nav, Dropdown } from "react-bootstrap";

导航栏

<Navbar
    sticky="top"
    id="navbar"
    bg="light"
    expand="lg"
    className="navbar navbar-expand-lg navbar-light bg-light"
    collapseOnSelect={true}
>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
    <Navbar.Collapse id="basic-navbar-nav">
        <Nav className="ml-auto">
            <Link
                activeClass="active"
                to="features"
                spy={true}
                smooth={true}
                offset={-70}
                duration={800}
                className="nav-link"
                onClick={this.closeNavbar}
            >
                Features
            </Link>

            <Link
                activeClass="active"
                to="about"
                spy={true}
                smooth={true}
                offset={-70}
                duration={800}
                className="nav-link"
            >
                About
            </Link>
        </Nav>
    </Navbar.Collapse>
</Navbar>


推荐答案

出现了同样的问题。我发现,如果我们为Nav.Link项目添加 eventKey,则 collapseOnSelect将起作用

Had the same issue. I found that "collapseOnSelect" works if we add "eventKey" for Nav.Link item

示例:

import { Link } from 'react-router-dom';
import { Nav, Navbar} from 'react-bootstrap';

 <Navbar collapseOnSelect  expand="lg">
      <Navbar.Toggle />
      <Navbar.Collapse>
        <Nav className="mr-auto d-block">
          <Nav.Item>
            <Nav.Link eventKey="1" as={Link} to="/Home">
              Home
            </Nav.Link>
          </Nav.Item>
          <Nav.Item>
             <Nav.Link eventKey="2" as={Link} to="/Contant">
              Page Contant
            </Nav.Link>
          </Nav.Item>
        </Nav>
      </Navbar.Collapse>
    </Navbar>

这篇关于React Bootstrap导航栏崩溃不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:18