Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 需要使用的文件也需要_Javascript_Jquery_Requirejs - Fatal编程技术网

Javascript 需要使用的文件也需要

Javascript 需要使用的文件也需要,javascript,jquery,requirejs,Javascript,Jquery,Requirejs,我编写了一个jQuery插件,并使用require.js。我的plugin.js文件如下所示: require(['path/to/something'], function(Something) { // LINE 1 <-------- jQuery.fn.myPlugin = function(elem) { var s = new Something(elem); elem.data('something', s);

我编写了一个jQuery插件,并使用require.js。我的plugin.js文件如下所示:

require(['path/to/something'], function(Something) {
    // LINE 1 <--------
    jQuery.fn.myPlugin = function(elem) {
        var s = new Something(elem);
        elem.data('something', s);
        return elem;
    };
});
require(['path/to/something'],函数(something){

//第1行你应该在plugin.js中使用
define
而不是
require
?这里似乎有一个不必要的层。如果模块是jQuery插件,它应该在其定义函数中附加到jQuery对象,而不需要自己的代码。@metadept:我试图在插件代码之间进行分离和网站使用代码。我不能将jQuery.fn.myPlugin=func…放入index.html中。稍后我将使用require.js优化()为了将所有插件文件转换为一个文件。除此之外,我不需要在plugin.js中定义,因为我没有任何东西可以返回。同意@metadept。您的模块不应该以
define
开始,并且依赖于
jquery
?@PaulGrime:当我使用define时,在使用almond组合require.js文件之后,模块没有加载。。
   require(['js/plugin'], function() {
        $(function() {
            // LINE 2 <--------
            $('#elem').myPlugin();
        });
    });