在我的代码中,test.js 依赖于不使用 require AMD 模式的 jquery-ui,而 test.spec.js 依赖于使用 AMD 模式的 jquery-ui、test.js。运行test.spec.js时能否在test.js中动态加载jquery-ui的依赖。
require.config({
baseUrl: '/demo',
paths: {
'jquery': '../library/jquery-1.11.1',
'jquery-ui': '../library/jquery-ui-1.11.4'
},
shim: {
'jquery': {
exports: 'jQuery'
},
'jquery-ui': {
deps: ['jquery']
},
'library/src/js/test': {
deps: ['library/jquery-1.11.1', 'library/jquery-ui-1.11.4', '../js/collapse'],
exports: 'Test'
}
},
callback: window.__karma__.start
});
在 test.js 中编写了 jquery-ui 可拖动事件的“可拖动”。在评估 $('#panelId').draggable({revert: true});
之后
出错了
"TypeError: 'undefined' is not a function (evaluating '$('#panelId').draggable({revert: true})')"
如何在 require.config 中为 test.js 加载 jquery-ui。因为我正在使用它来运行我的 Jasmine 测试用例。 在真实环境中,它按预期工作,但在 jasmine 测试用例中无法找到 jquery-ui 事件。 test.js 没有使用 require.js,但 test.spec.js 使用 require AMD 模式。
执行后在 test.spec.js 代码中出现 jquery-ui draggable undefined 错误
define(['jquery-ui','library/src/js/test'], function ($) {
});
我可以使用 $ 在 test.spec.js 中访问 jquery ui,而不是在 test.js 中访问 jquery-ui 事件,因为 test.js 不使用 AMD require 模式。不知道缺少什么。任何帮助都会得到帮助... :)
最佳答案
试试这个更新的 require js 配置
使用“library/src/js/test”加载你的测试
require.config({
baseUrl: '/demo',
paths: {
'jquery': '../library/jquery-1.11.1', // assuming this is a correct path
'jquery-ui': '../library/jquery-ui-1.11.4' // assuming this is a correct path
},
shim: {
'jquery': {
exports: 'jQuery'
},
'jquery-ui': {
deps: ['jquery']
},
'library/src/js/test': {
deps: ['jquery', 'jquery-ui', '../js/collapse'], // changes done here
exports: 'Test'
}
},
callback: window.__karma__.start
});
关于javascript - 要求使用 AMD 模式为 jQuery UI 事件提供错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31940860/