简体   繁体   English

如何在angular cli 1.0.0-rc.0中正确包含jQuery?

[英]How to include jQuery properly in angular cli 1.0.0-rc.0?

I use jQuery-based select2 component in my AngularJS project. 我在AngularJS项目中使用基于jQuery的select2组件。 I had similar issue as guys here: https://github.com/fronteed/icheck/issues/322 , and solved it using advice from there. 我和这里的人有类似的问题: https//github.com/fronteed/icheck/issues/322 ,并使用那里的建议解决了它。 To be accurate, I received error TypeError: $(...).select2 is not a function when not using that advice. 为了准确,我收到了错误TypeError: $(...).select2 is not a function当不使用该建议时, TypeError: $(...).select2 is not a function

ie I added next lines to configuration of Webpack in @angular/cli/models/webpack-configs/common.js . 即我在@angular/cli/models/webpack-configs/common.js添加了下一行到@angular/cli/models/webpack-configs/common.js

plugins: [
  new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    })
]

Is it the best possible way to enable jQuery in angular/cli? 它是在angular / cli中启用jQuery的最佳方法吗?

I don't think that doing same as in https://github.com/angular/angular-cli/wiki/stories-global-lib is correct way, because 我不认为在https://github.com/angular/angular-cli/wiki/stories-global-lib中做同样的事情是正确的,因为

a) webpack bundles jQuery without need to specify it in scripts a)webpack捆绑jQuery而无需在脚本中指定它

b) it still throws TypeError: $(...).select2 is not a function error when you include it as described in story. b)它仍然抛出TypeError: $(...).select2 is not a function如故事中所述,当你包含它时, TypeError: $(...).select2 is not a function错误。

i use jQuery in my project as follows. 我在我的项目中使用jQuery如下。

  1. Install jQuery 安装jQuery

     npm install --save jquery 
  2. Install jQuery type defination for type checking. 安装jQuery类型定义以进行类型检查。

     npm install --save-dev @types/jquery 
  3. Add refenece of jquery file in "scripts" array inside angular-cli.json file. 在angular-cli.json文件中的“scripts”数组中添加jquery文件的refenece。

     "scripts": [ "../node_modules/jquery/dist/jquery.min.js" ] 
  4. import jquery in any component you want to use. 在要使用的任何组件中导入jquery。

     import * as jQuery from 'jquery'; 

that's it. 而已。 the same way you can also use other libraries like moment.js , d3.js etc. 同样你也可以使用其他库,如moment.jsd3.js等。

You could also use expose-loader . 您也可以使用expose-loader The example below is written for webpack 2, 以下示例是针对webpack 2编写的,

{ test: /[\/]jquery\.js$/,
    use: [
      { loader: 'expose-loader', query: 'jQuery' },
      { loader: 'expose-loader', query: '$' }
    ]
  },

Adding it in the same way as the link you provided will guarantee it's loaded. 以与您提供的链接相同的方式添加它将保证它已加载。 Otherwise try below in your component. 否则请在您的组件中尝试下面。

npm install jquery

Then use in your component 然后在您的组件中使用

declare var $:any;
var $ = require('jquery');

I was able to achieve what I needed by exposing Webpack. 通过暴露Webpack,我能够实现我所需要的。 Use ng eject command. 使用ng eject命令。

First, install jQuery using npm install -S jquery (and I also installed npm install -S select2 , because I needed that too). 首先,使用npm install -S jquery (我还安装了npm install -S select2 ,因为我也需要它)。

Then, make sure that you made backup of your package.json file, since during ng eject Angular CLI will try to add its Webpack dependencies inside your package.json. 然后,确保您备份了package.json文件,因为在ng eject期间,Angular CLI将尝试在package.json中添加其Webpack依赖项。

After ng eject finished working, inside webpack.config.js I added ng eject完成后,在webpack.config.js里面我添加了

// ... Inside require/imports part
const ProvidePlugin = require('webpack/lib/ProvidePlugin');

// ... Inside `plugins` section of Webpack configuration
new ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    })

And now select2 adds $(...).select2 function to global jQuery object properly! 现在select2正确地将$(...).select2函数添加到全局jQuery对象中!

To use jQuery with select2 inside your .ts file, you can add next lines at the beginning of that file: 要在.ts文件中使用带有select2的jQuery,可以在该文件的开头添加下一行:

import "select2";
import "jquery";
declare var $: any;

// Somewhere deep within component
// ...

$(this.input.nativeElement)
    .select2(config)
    .on("change select2:select select2:unselect", (e: any) => this.onSelect2ElementChange(e));

// ...

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

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