Using jquery and other plugins with angular-cli rc0 in angular2

核能气质少年 提交于 2019-12-22 14:00:09

问题


I've a angular project created using @angular/cli-1.0.0-rc0. I need to use: - jquery - a query widget (in my example http://eonasdan.github.io/bootstrap-datetimepicker/).

Using 'webpack-bundle-analyzer' I can see that jquery is imported in vendor.bundle.js (if imported in a ts file) but not globally exposed, so jquery plugin fails to load itself.

Is there a way to:

  • load jquery (and others libraries such as momentjs) only in scripts bundle?
  • do not add jquery in vendor bundle but add it only as reference?

If I put jquery between scripts:[] section in .angular-cli.json

  • jquery library is added both in vendor and scripts bundles.
  • $ instance in typescript file is not the one exposed globally, so the plugin is available in window.$ but it is not available in angular directive.

In previous versions of angular-cli, a solution found on stackoverflow was to:

  • create a vendor.ts with:
    • import * as jquery from 'jquery';
    • window['jQuery'] = window['$'] = jquery;
  • add vendor.ts in scripts: [] structure

but this solution apparently doesn't work anymore. I receive a webpack error during compilation.


At the moment, as a workaround:

  • jquery, moment, eonasdan datetime picker... added in scripts[]
  • jquery.. not imported in typescript files (just declared as "declare var $:any". I don't like it, but I cannot find any other solutions. Also this one (How to include jQuery properly in angular cli 1.0.0-rc.0?) doesn't work for me.

来源:https://stackoverflow.com/questions/42601352/using-jquery-and-other-plugins-with-angular-cli-rc0-in-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!