简体   繁体   English

Webpack在其他组件中包含jQuery

[英]Webpack include jQuery in other components

I use Webpack and my scripts is structured on this way. 我使用Webpack,并且我的脚本是按这种方式构造的。

scripts
  components
    heroSlider.js
  app.js

I install jQuery via npa npm and import in app.js like this 我通过npa npm安装jQuery并像这样导入app.js

import $ from 'jquery';

And it's work fine. 而且工作正常。 But i wan't to use jQuery in my component file. 但是我不想在我的组件文件中使用jQuery。 How? 怎么样?

In app.js i also import component. app.js中,我还导入了组件。

import heroSlider from './components/heroSlider';

heroSlider.js look like this heroSlider.js看起来像这样

export default function heroSlider() {

  // Here I wan't to use jQuery

}

But I don't want import jQuery again in component when i want to use it. 但是我不想在我想使用它时再次在组件中导入jQuery。 There is a way to do that ? 有办法吗?

If you're using jquery plugins that require a global jquery instance, you can expose jquery using expose-loader 如果您使用需要全局jquery实例的jquery插件,则可以使用暴露加载器 公开 jquery

Once you set it up you'll be able to require it using: 设置好之后,您可以使用以下方法进行要求:

require("expose?$!jquery");

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

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