我是ReactJS的新手,我想在我的React应用程序中包括引导

我已经通过npm安装bootstrap -save安装了bootstrap

现在,我想在React应用程序中加载引导CSS和JS。

我正在使用webpack。

webpack.config.js

var config = {
    entry: './src/index',

    output: {
        path: './',
        filename: 'index.js'
    },

    devServer: {
        inline: true,
        port: 8080,
        historyApiFallback: true,
        contentBase:'./src'
    },

    module: {
        loaders: [
            {
                test: /\.js?$/,
                exclude: /node_modules/,
                loader: 'babel',

                query: {
                    presets: ['es2015', 'react']
               }
            }
        ]
    }
};

module. exports = config;

我的问题是“如何从节点模块中包含引导CSS和JS在ReactJS应用程序中?”如何设置引导包括在我的React应用程序?


当前回答

我也有过类似的经历。我的设置如下所示

npm install create-react-app

这将为您提供一个开始的样板代码设置。在App.js文件中使用主逻辑。

稍后,您可以在CLI工具创建的index.html中使用引导CDN。 或

npm install bootstrap popper jquery

然后简单地将其包含在App.js文件中。

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap/dist/js/bootstrap.bundle'

很少有作者提到使用className属性而不是class,但在我的例子中,两者都像下面这样工作。但是如果你使用class并在浏览器的开发工具中查看控制台,你会看到使用class的错误。所以改用className。

    <div className="App" class = "container"> //dont use class attribute

不要忘记删除默认的CSS导入,以避免与引导程序冲突。

希望能有所帮助,编码愉快!!

其他回答

因为Bootstrap/Reactstrap已经发布了他们的最新版本,即Bootstrap 4,你可以按照以下步骤使用它

导航到您的项目 打开终端 我假设已经安装了npm,然后输入以下命令 NPM install——save react-dom

这将在您的项目中作为依赖项安装Reactstrap。

下面是使用react创建按钮的代码

import React from import {Button} from 'reactstrap'; 导出默认值(props) => { 回报( <按钮颜色= "危险" >危险!< / >按钮 ); };

你可以通过访问他们的官方页面来检查Reactstrap

请按照下面的链接使用React引导。 https://react-bootstrap.github.io/

安装:

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

------------------------------------ 或 -------------------------------------

把Bootstrap CDN for CSS在index.html文件的标签在react和Bootstrap CDN for Js在index.html文件的标签。使用className而不是class 遵循下面的index.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="theme-color" content="#000000">

    <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
    <link rel="shortcut icon" href="../src/images/dropbox_logo.svg">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">




  </head>
  <body>

    <div id="root"></div>

     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>

  </body>
</html>

简单解决方案(2020)如下:-

保存jquery popper.js NPM install -save bootstrap 样式加载器css-loader 更新webpack.config.js,你必须告诉webpack如何处理引导CSS文件,因此你必须添加一些规则,如所示

在你的React应用程序的入口点(通常是index.js)添加一些导入,把它们放在顶部,确保你没有改变顺序。

导入“bootstrap/dist/css/bootstrap.min.css”;

从jquery中导入$;

import Popper from " Popper .js";

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

这些步骤应该可以帮助你,如果有什么问题请评论。

某种程度上,公认的答案是只谈论包括css文件从bootstrap。

但是我认为这个问题和这个有关 引导下拉在React中不工作

有几个答案可以帮助你

https://stackoverflow.com/a/52251319/4266842 https://stackoverflow.com/a/54188034/4266842

以防万一,如果你可以使用React应用程序推荐使用的yarn,

你可以,

yarn add bootstrap@4.1.1 // this is to add bootstrap with a specific  version

这将把引导程序作为依赖项添加到包中。Json也是。

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "bootstrap": "4.1.1", // it will add an entry here
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-scripts": "1.1.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

然后在index.js文件中导入bootstrap。

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