jspm:导入jQuery插件时出错 [英] jspm: Error when importing a jQuery plugin

查看:101
本文介绍了jspm:导入jQuery插件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导入jQuery插件(即 https://github.com/Mottie/Keyboard)使用jspm/SystemJS.

I'm trying to import a jQuery plugin (namely https://github.com/Mottie/Keyboard) using jspm/SystemJS.

首先,我只需输入以下命令即可安装模块:

At first, I installed the module by simply typing the following command:

jspm install github:mottie/keyboard

然后,在导入jQuery之后,我在代码中添加了要导入库的行:

I then added the line to import the library in my code, just after importing jQuery:

import keyboard from 'mottie/keyboard';

但是,在运行代码时,遇到以下错误:

However, when running the code, I encountered the following error:

Uncaught TypeError: Multiple defines for anonymous module

谷歌搜索错误并没有给我提供解决方案,至少没有一个我能理解的解决方案... 我不知道附近是否有一些jspm专家可以帮助我? :)

Googling the error didn't provide me with a solution, at least not one that I could understand... I don't know if there are some jspm gurus around here that could help me? :)

非常感谢...

推荐答案

如果您查看jQuery.keyboard的源代码,它将在代码中两次使用UMD模式:

If you look at the source for jQuery.keyboard, it uses a UMD pattern twice in the code:

一次在 https://github.com/Mottie/Keyboard/blob/master/js/jquery.keyboard.js#L31 ,然后在 SystemJS将文件检测为AMD,但它自己定义了两次,而不是一次.

SystemJS is detecting the file as AMD, but it is defining itself twice instead of once.

因此,这基本上不是有效的AMD模块,因此您需要告诉SystemJS将其视为全局模块.

Basically as a result this isn't a valid AMD module, so you need to tell SystemJS to treat it as a global instead.

这可以通过覆盖来实现:

This can be done with an override:

jspm install github:mottie/keyboard -o "{format: 'global'}"

即使如此,以上内容也要求jQuery已被加载.为此,我们可以在jQuery上添加垫片以强制执行依赖性.

Even then, the above requires that jQuery is already loaded. For this we can add a shim on jQuery to enforce the dependency.

带有shim的标准jQuery插件覆盖如下:

The standard jQuery plugin override with a shim looks like:

override.json

override.json

{
  "main": "js/jquery.keyboard.js",
  "shim": {
    "js/jquery.keyboard": {
      "deps": ["jquery"]
    }
  },
  "dependencies": {
    "jquery": "*"
  }
}

然后我们可以通过以下方式安装它:

We can then install this with:

jspm install github:mottie/keyboard -o override.json

如果可以,请将替代内容发布到 jspm注册表,这样其他用户也可以从中受益.

Do post your override to the jspm registry if it works out and then other users can benefit as well.

这篇关于jspm:导入jQuery插件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆