gpt4 book ai didi

javascript - 如何在 ES6 中调用 bootstrap 的 jQuery 插件

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:27 25 4
gpt4 key购买 nike

我一直在尝试调用 Bootstrap 的 jQuery 插件,例如弹出窗口、工具提示、模态……

我正在使用 webpack,这是我的 ES6 javascript:

import $ from 'jquery';
//import Bootstrap from 'bootstrap-sass';
//import Bootstrap from 'bootstrap-loader';
import Bootstrap from '../vendor/bootstrap.min.js';

class Test {
constructor () {
$('[data-toggle="tooltip"]').tooltip({ html: true });
}
}

我尝试使用 npm 安装 bootstrap,但之后我不确定我必须导入哪个节点模块(就像您在导入的注释行中看到的那样)。所以,我想直接导入 bootstrap.min.js

事实是,我的 app.js 中仍然有这样的错误(如果我尝试使用 popover/modals/tooltip,这是独立的),这是我从 webpack 生成的 javascript:

Uncaught TypeError: (0 , _jquery2.default)(...).tooltip is not a function

就像我说的,我被困在这里了。

最后,boostrap CSS 的正常工作归功于此:

gulp.task('bootstrap-fonts', function() {
return gulp.src('node_modules/bootstrap-sass/assets/fonts/bootstrap/*')
.pipe(gulp.dest('./app/assets/fonts/bootstrap'));
});

gulp.task('dev', ['css', 'bootstrap-fonts', 'browser-sync', 'webpack'], function () {
gulp.watch('src/scss/**/*.scss', ['css']);
gulp.watch('src/js/**/*.js', ['webpack']);
gulp.watch('app/*.html', ['bs-reload']);
});

最佳答案

因为bootstrap库依赖于jQuery,您应该尝试将以下插件添加到 webpack.config.js 中的“插件”数组,以便 Bootstrap 模块将使用 jQuery 全局对象:

new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"window.jQuery": 'jquery'
}),

这个插件实际上会将“jquery”模块注入(inject)到任何其他需要他的模块中(也就是说,每个使用对象 jQuery 或 $ 或 window.jQuery 的模块),bootstrap 就是其中之一。

关于javascript - 如何在 ES6 中调用 bootstrap 的 jQuery 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39127511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com