bootstrap&导航栏崩溃不起作用(create-react-app)

时间:2018-01-14 11:11:48

标签: twitter-bootstrap reactjs

我正在尝试使用:bootstrap 4 navbar,它不会崩溃反应。 https://getbootstrap.com/docs/4.0/components/navbar/#supported-content

有人可以解释为什么吗? 我假设这可能是因为需要修改组件来改变状态?但这只是我的猜测。

import React from "react";
import ReactDOM from "react-dom";

import registerServiceWorker from "./registerServiceWorker";
// styles
import "../node_modules/jquery/dist/jquery.min.js";
import "../node_modules/bootstrap/dist/css/bootstrap.min.css";

ReactDOM.render(
<nav className="navbar navbar-expand-lg navbar-light bg-light">
  <a className="navbar-brand" href="#">Navbar</a>
  <button  className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span  className="navbar-toggler-icon"></span>
  </button>

  <div  className="collapse navbar-collapse" id="navbarSupportedContent">
    <ul  className="navbar-nav mr-auto">
      <li  className="nav-item active">
        <a  className="nav-link" href="#">Home <span  className="sr-only">(current)</span></a>
      </li>
      <li  className="nav-item">
        <a  className="nav-link" href="#">Link</a>
      </li>
      <li  className="nav-item dropdown">
        <a  className="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div  className="dropdown-menu" aria-labelledby="navbarDropdown">
          <a  className="dropdown-item" href="#">Action</a>
          <a  className="dropdown-item" href="#">Another action</a>
          <div  className="dropdown-divider"></div>
          <a  className="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li  className="nav-item">
        <a  className="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form  className="form-inline my-2 my-lg-0">
      <input  className="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"/>
      <button  className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>
, document.getElementById("root"));
registerServiceWorker();

4 个答案:

答案 0 :(得分:0)

导入bootstrap.js后尝试在import中添加jquery

import "../node_modules/jquery/dist/jquery.min.js";
import "../node_modules/bootstrap/dist/js/bootstrap.min.js";

答案 1 :(得分:0)

如果是Bootstrap v3,可能会尝试将班级navbar-toggler更改为navbar-toggle

在npm页面上Bootstrap 3.3.7是最新版本..

答案 2 :(得分:0)

对我而言,最好的解决方案是仅在navbar组件中导入bootstrap crash.js脚本,以防止混合js脚本。

将此内容包括在navbar.jsx组件的顶部:

import "bootstrap/js/src/collapse.js";

答案 3 :(得分:0)

在bootstrap.min.css之后简单地导入bootstrap.min.js

import "bootstrap/dist/js/bootstrap.min.js";