简体   繁体   English

在将Bootstrap与Webpack结合使用时,为什么需要jQuery的特殊“ hack”

[英]Why do I need a special “hack” for jQuery when using Bootstrap with Webpack

My original question was solved by reading this thread , but since I want to understand the whole webpack system, I ask myself: 我最初的问题是通过阅读此线程解决的,但是由于我想了解整个Webpack系统,所以我问自己:

Why do I have to do this? 为什么我必须这样做? (Import jQuery myself and explicitly include it in some way in the webpack config) (自己导入jQuery,并以某种方式将其明确包含在webpack配置中)

Shouldn't webpack find jQuery as a dependency of bootstrap and add it, when I import bootstrap? 当我导入引导程序时,webpack是否不应该将jQuery作为引导程序的依赖项并添加它? I just don't understand why there is so much happening automagically, but this is breaking without me hacking something into my webpack config. 我只是不明白为什么会有如此多的事情自动发生,但是如果没有在我的webpack配置中入侵某些东西,这一切就破裂了。

This is basically my setup: 这基本上是我的设置:

  • cloned the angular2 seed which uses webpack: https://github.com/angular/angular2-seed 克隆了使用webpack的angular2种子: https : //github.com/angular/angular2-seed

  • installed bootstrap package: npm install bootstrap --save ( => "bootstrap": "^3.3.7") 已安装的引导程序软件包:npm install bootstrap --save(=>“ bootstrap”:“ ^ 3.3.7”)

  • imported the bootstrap css in my app.component.ts: import 'bootstrap/dist/css/bootstrap.css'; 在我的app.component.ts中导入了引导CSS:import'bootstrap / dist / css / bootstrap.css';

The css seems to be imported just fine, but I get this "Uncaught ReferenceError: jQuery is not defined" (at run time) which also appears in the thread mentioned above. css似乎可以很好地导入,但是我得到了这个“ Uncaught ReferenceError:未定义jQuery”(在运行时),它也出现在上述线程中。

My question is admittedly a similar one to this one but I don't think the answers have been really convincing. 我的问题被公认为与问题类似,但我认为答案并不令人信服。

When modules use jQuery, shouldn't they import jQuery in some way? 当模块使用jQuery时,它们不应该以某种方式导入jQuery吗?

Or, asked in another way: If I get this runtime error, doesn't it mean the contrib module's devs worked a bit dirty? 或者,以另一种方式问:如果出现此运行时错误,这并不意味着contrib模块的开发人员的工作有点肮脏吗?

Please consider that I am 请认为我是

  • new to 1. stackoverflow, 2. webpack 1。stackoverflow,2。webpack新

  • quite new to modular JavaScript 模块化JavaScript的新手

  • not interested in a concrete problem being solved, rather than in general enlightenment regarding this topic 对解决的具体问题不感兴趣,而不是对此主题的一般启示

Thanks in advance 提前致谢

Cheers Sören 干杯

I agree. 我同意。 The bootstrap PR #16534 CommonJS: require() jQuery if not already loaded tried to change the behaviour (ie, automatically require jQuery unless present), but has been closed since no more development is happening for bootstrap 3. 引导程序PR #16534 CommonJS:require()尚未加载的jQuery试图更改行为(即,除非存在,否则自动require jQuery),但已被关闭,因为引导程序3不再进行开发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM