简体   繁体   English

Bootstrap 在 create-react-app 中不起作用

[英]Bootstrap is not working in create-react-app

在此处输入图像描述 I am trying to use this nav bar while it seems classes are not working: I did all the steps like it was described in the documentation.我正在尝试使用此导航栏,而似乎类不起作用:我执行了文档中描述的所有步骤。 I've imported it in the index.js:我已经在 index.js 中导入了它:

import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';


ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

Then I tried to implement nav bar with its classes but seems very ugly and not working at all.:然后我尝试用它的类来实现导航栏,但看起来很丑而且根本不工作。:

<nav className="navbar navbar-expand-lg navbar-dark navbar-custom fixed-top">
      <div className="container">
        <a className="navbar-brand" href="#">Start Bootstrap</a>
        <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          <span className="navbar-toggler-icon"></span>
        </button>
        <div className="collapse navbar-collapse" id="navbarResponsive">
          <ul className="navbar-nav ml-auto">
            <li className="nav-item">
            <Link to="/">Home<span classNameName="sr-only">(current)</span></Link>
            </li>

           <li classNameNameName="nav-item">
       <Link to="/signin">Sign In</Link>
      </li>
      <li classNameNameName="nav-item">
      <Link to="/signup">Sign Up</Link>
      </li>
          </ul>
        </div>
      </div>
    </nav>

As you see in the image it was not the real nav bar that should be visible.正如您在图像中看到的那样,它不是应该可见的真正导航栏。 Another point is that after generating this app there is no index.html file at all.还有一点是,在生成这个应用程序之后,根本就没有 index.html 文件。 So the only place where I import bootstrap is index.js How can I fix this to make nav bare work correctly with bootstrap?所以我导入 bootstrap 的唯一地方是 index.js 如何解决这个问题以使 nav bare 与 bootstrap 一起正常工作?

Since you have installed the bootstrap package in you app module, no need to give the relative path there. 由于您已在app模块中安装了引导程序包,因此无需在此处提供相对路径。 you can import it by just below.. 你可以在下面导入它..

index.js index.js

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

OR 要么

you can import the bootstrap file in you index.css file, ie 你可以在index.css文件中导入bootstrap文件,即

index.css index.css

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

or 要么

import '~node_modules/bootstrap/dist/css/bootstrap.min.css';

Now you are adding a css file, so make sure you have configured a css loader in webpack configuration. 现在您要添加一个css文件,因此请确保在webpack配置中配置了一个css加载器。

install bootstrap npm and install react-dom if not install. 如果没有安装,请安装bootstrap npm并安装react-dom。 command are here 命令在这里

 npm install --save react react-dom
 npm install --save react-bootstrap

now add css file in your_project_name->public>index.html 现在在your_project_name-> public> index.html中添加css文件

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

import bootstrap module which you required in your component eg is below 您在组件中需要的导入引导模块,例如下面

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

if you need a video tutorial here is link react-js bootstrap video tutorial 如果你需要一个视频教程这里是链接react-js bootstrap视频教程

Alternate option, I'm using Original Bootstrap Framework this way.替代选项,我以这种方式使用原始引导框架。

Add bootstrap by npm install bootstrap通过npm install bootstrap添加引导程序

in App.js imports css and jsApp.js中导入 css 和 js

import 'bootstrap/dist/css/bootstrap.css';
import "bootstrap"; // <-- JS File

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM