使用jQuery作为依赖项,而无需使用RequireJS加载jQuery? [英] Use jQuery as a dependency without loading jQuery with RequireJS?

查看:46
本文介绍了使用jQuery作为依赖项,而无需使用RequireJS加载jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下页面:

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
</head>
<body>
    <script data-main="app" src="require.js"></script>
</body>
</html>

未将RequireJS加载时,如何使用jQuery作为RequireJS模块的依赖项?我知道 jQuery将自己暴露为全局AMD模块,所以我应该能够将其添加为依赖项,但我不确定如何添加.

How can i use jQuery as a dependency for RequireJS modules when it's not loaded with RequireJS? I know jQuery exposes itself as a global AMD module, so i should be able to add it as a dependency, but i'm not quite sure how to.

我希望能够执行以下操作:

I would like to be able to do the following:

require(['jquery'], function() {
    console.log('success')
});

如果jQuery尚未完成加载,则应等待它完成,然后继续.这可能吗?

And if jQuery has not yet finished loading, it should wait for it to complete, and then continue. Is this possible to do?

推荐答案

第二次加载jQuery,首先加载RequireJS.加载完两个模块后运行主模块.

Load jQuery second, RequireJS first. Run your main module when both are loaded.

<html>
<head>
    <script src="require.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
</head>
<body>
    <script>
        require(["app"], function(app){
            app.runMe()
        })
    </script>
</body>
</html>

如果

jQuery没有看到define.amd浮动,则不会将其自身注册为AMD模块.使其正确注册的唯一方法是在jQuery之前加载RequireJS.

jQuery will NOT register itself as AMD module if it does not see define.amd floating around. The only way to make it register itself properly is to load RequireJS before jQuery.

这篇关于使用jQuery作为依赖项,而无需使用RequireJS加载jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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