简体   繁体   English

如何在Angular 2 + / Webpack / Angular CLI中将jQuery导入jQuery插件

[英]How to import jQuery into a jQuery plugin in Angular 2+ / Webpack / Angular CLI

I'm trying to import a jQuery plugin into a single Angular component. 我正在尝试将jQuery插件导入单个Angular组件。 It runs in every other browser, but IE 11 chokes on it: 它可以在所有其他浏览器中运行,但是IE 11却使它窒息:

SCRIPT1002: Syntax error
main.bundle.js (1376,1)

When I click the error, it shows me the line at issue: 当我单击该错误时,它显示出有问题的行:

eval("Object.defineProperty(__webpack_exports__, \"__esModule\", { value: true });\n/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_jquery__ = __webpack_require__(\"../../../../jquery/dist/jquery.js\");\n/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0_jquery___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0_jquery__);\n/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__angular_core__ = __webpack_require__(\"../../../core/esm5/core.js\");\n// require('jquery');\n\n\n\n/*\ndjaodjin-annotate.js...

That's not all of it, but it appears to be to do with importing jQuery, and indeed, when I remove the jQuery import, it runs fine. 这还不是全部,但似乎与导入jQuery有关,的确,当我删除jQuery导入时,它运行良好。

The issue may be that I'm importing jQuery into a JavaScript (.js) file, which is a jQuery plugin. 问题可能是我正在将jQuery导入到JavaScript(.js)文件中,该文件是jQuery插件。 Here's a simplified version of it: 这是它的简化版本:

import * as jQuery from 'jquery';

(function($) {
  'use strict';

  function Annotate(el, options) {
    this.init();
  }
  Annotate.prototype = {
    init: function() {
      console.log('It\'s working.');
    },
  };
  $.fn['annotate'] = function() {

    const annotate = new Annotate($(this));

    return annotate;
  };
})(jQuery);

If I can't import jQuery into a jQuery plugin, how can I use a jQuery plugin? 如果无法将jQuery导入jQuery插件,如何使用jQuery插件? Is there a way around this? 有没有解决的办法?

BTW, I'm using jQuery only in one component. 顺便说一句,我只在一个组件中使用jQuery。 The rest of the application is clean of it. 该应用程序的其余部分均已清除。

I'd say you don't need to import jQuery in your plugin; 我想说您不需要在插件中导入jQuery。 your plugin should just assume that jquery has been included and fail if it has not 您的插件应仅假设已包含jquery,如果未包含,则失败

You can include jquery (first) then your plugin file into your project. 您可以先包含jquery,然后将插件文件包含到项目中。 Then in your component just declare jquery 然后在您的组件中声明jquery

declare let $ : any;

and instantiate your plugin like 并实例化您的插件

$('div').annotate();

I actually found that my jQuery plugin had errors in it. 我实际上发现我的jQuery插件有错误。 It had a couple of lines with ES6 syntax that IE didn't like. 它有几行IE不喜欢的ES6语法。 But when Webpack compiles it, it turns the whole thing into a string, all on one line. 但是,当Webpack对其进行编译时,它将整个东西变成一个字符串,全部都在一行上。 At runtime, it then runs eval() on the string. 在运行时,然后在字符串上运行eval() So you don't get valuable error messages with line numbers. 因此,您不会获得带有行号的有价值的错误消息。 It just gives you the stupid error that I got. 它只是给您我得到的愚蠢错误。 But I think @David has a good point. 但是我认为@David有一个好处。 We maybe don't need to import jQuery. 我们也许不需要导入jQuery。

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

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