【问题标题】:How to import bootstrap 5 bundle js with npm?如何使用 npm 导入 bootstrap 5 bundle js?
【发布时间】:2021-03-16 18:10:34
【问题描述】:

我对 bootstrap5 和 NPM 有一些问题。 网站设计使用 bootstrap 并且可以工作,但不是所有的 js(下拉菜单、模式等),我只是想学习如何在没有 CDN 的情况下导入 js bs 包。 有人可以帮助我吗?感谢您的宝贵时间

我的步骤:

npm install bootstrap@5.0.0-alpha1
npm install popper.js --save
npm intsall node-sass --save-d

Package.json:

{
  "name": "xlf",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "./node_modules/.bin/webpack --watch",
    "sass": "node-sass --watch src/scss -o dist/css --output-style nested"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bootstrap": "^5.0.0-alpha1",
    "popper.js": "^1.16.1"
  },
  "devDependencies": {
    "node-sass": "^5.0.0",
    "webpack": "^5.26.0",
    "webpack-cli": "^4.5.0"
  }
}

webpack.config.js

const path = require('path');

module.exports = {
   entry: './src/js/app.js',
   output: {
      path: path.resolve(__dirname, 'dist','js'),
      filename: 'app.js',
   },
   mode: 'production',
   module: {
      rules:[
       {
          test: /.(scss|css)$/,
          use:
          [
             {
                options: { reloadAll: true },
             },
             'css-loader',
             'sass-loader',
             'node-sass'
          ]
       }
      ]
   }
 };

src/js/app.js

import 'bootstrap';

src/scss/app.scss

@import '../../node_modules/bootstrap/scss/bootstrap.scss';

【问题讨论】:

  • 您使用的是alpha 版本。这可能是原因。试试npm i -S bootstrap@5.0.0.beta2
  • 可以在node_modules import './node_modules/bootstrap/dist/js/bootstrap.min.js'中缩小JS文件的范围
  • @jaba: 尝试使用 beta 版本但结果相同:(
  • 结果如何?
  • modal, dropdown ecc not working...我的代码 JS 只有在我也通过 CDN 添加到 HTML popper 时才有效

标签: javascript npm bootstrap-5


【解决方案1】:

通过以下步骤解决:

  1. 安装测试版
  2. 卸载 popper.js 并安装 @popperjs/core
  3. 在导入'bootstrap'之前添加;导入“@popper/core”;

感谢贾巴的帮助。

【讨论】:

  • 您可以尝试不执行第 3 步。我不确定是否有必要。需要安装@popperjs/corebootstrap 应该导入它。
猜你喜欢
  • 2023-02-22
  • 1970-01-01
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
  • 2021-03-02
  • 1970-01-01
相关资源
最近更新 更多