gpt4 book ai didi

jquery - jspm:导入 jQuery 插件时出错

转载 作者:行者123 更新时间:2023-12-03 22:59:28 25 4
gpt4 key购买 nike

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

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

jspm install github:mottie/keyboard

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

import keyboard from 'mottie/keyboard';

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

Uncaught TypeError: Multiple defines for anonymous module

谷歌搜索错误并没有给我提供解决方案,至少没有一个我能理解的解决方案......不知道这里有没有jspm高手可以帮助我? :)

提前非常感谢...

最佳答案

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

一次在https://github.com/Mottie/Keyboard/blob/master/js/jquery.keyboard.js#L31 ,并一次访问 https://github.com/Mottie/Keyboard/blob/master/js/jquery.keyboard.js#L2165 .

SystemJS 正在检测该文件为 AMD,但它自己定义了两次而不是一次。

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

这可以通过覆盖来完成:

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

即使如此,上面的内容也要求 jQuery 已经加载。为此,我们可以在 jQuery 上添加一个填充程序来强制执行依赖关系。

使用垫片覆盖的标准 jQuery 插件如下所示:

覆盖.json

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

然后我们可以安装它:

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

请将您的覆盖发布到 jspm registry如果成功,其他用户也可以受益。

关于jquery - jspm:导入 jQuery 插件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29148667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com