【发布时间】: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