简体   繁体   English

尝试通过npm加载jQuery插件

[英]Trying to load jQuery plugin via npm

I'm trying to load a jquery plugin in vuejs but nothing seems to be doing the trick. 我正在尝试在vuejs中加载一个jquery插件,但是似乎没有任何办法。

I have 我有

window.$ = window.jQuery = require('jquery');
// Plugin I'm trying to load
require('jcanvas');

The above part seems to work correctly (I think), but however when I try to use the plugin 上面的部分似乎正常工作(我认为),但是当我尝试使用插件时

$('canvas').drawArc({
            fillStyle: '#000',
            x: 100, y: 100,
            radius: 50
        });

It throws drawArc Is not a function error which seems like the plugin has not bee loaded in correctly or its not accessible. 引发drawArc Is not a function error ,似乎插件未正确加载drawArc Is not a function error或其不可访问。

import $ from 'jQuery'
import "jcanvas"

$('canvas').drawArc({
    fillStyle: '#000',
    x: 100, y: 100,
    radius: 50
});

I'm sure you've probably gotten past this, but I was having the same problem getting jCanvas to work with webpack. 我确定您可能已经克服了这一点,但是让jCanvas与Webpack一起使用时遇到了同样的问题。

The reason is that webpack treats loaded modules as immutable. 原因是webpack将加载的模块视为不可变的。 Jcanvas extends jquery functionality, so it won't work. Jcanvas扩展了jQuery功能,因此将无法使用。 What you need to do is externally link to jquery via a normal script call in your html file. 您需要做的是通过html文件中的常规脚本调用从外部链接到jquery。

<script
  src="https://code.jquery.com/jquery-3.1.0.js"
  integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="
  crossorigin="anonymous">
</script>

Then, you exclude jquery from webpack 然后,将jquery从webpack中排除

module.exports = {
  //...
  externals: {
    jquery: 'jQuery'
  }
}; 

Now you should be able to use jCanvas or any other plugin that extends jquery within a webpack project. 现在,您应该可以使用jCanvas或在Webpack项目中扩展jquery的任何其他插件。

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

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