我是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应用程序?


当前回答

以防万一,如果你可以使用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';

其他回答

通过npm,你会运行以下程序

npm install bootstrap jquery --save
npm install css-loader style-loader --save-dev

如果bootstrap为4,也添加依赖项popper.js

npm install popper.js --save

将以下内容(作为一个新对象)添加到webpack配置中

loaders: [
    {
      test: /\.css$/,
      loader: 'style-loader!css-loader'
    }

将以下内容添加到索引或布局中

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

我尝试指导:

npm install bootstrap
npm install jquery popper.js

然后在src/index.js中:

import 'bootstrap/dist/css/bootstrap.min.css';
import $ from 'jquery';
import Popper from 'popper.js';
import 'bootstrap/dist/js/bootstrap.bundle.min';
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

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

你可以在任何你想要的地方导入css文件。如果你根据需要配置了加载器,Webpack会注意捆绑css。

类似的,

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

webpack配置是这样的,

loaders: [{ test: /\.css$/, loader: 'style-loader!css-loader' }]

注意:你必须添加字体加载器,否则你会得到错误。

希望这是有帮助的;)

第一步:使用NPM安装以下命令

npm install --save reactstrap@next react react-dom

步骤2:示例index.js主脚本导入bellow命令

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

步骤3:UI组件参考以下网站 reactstrap.github.io

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

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

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

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

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

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