使用垫片Requirejs [英] Requirejs using shim

查看:174
本文介绍了使用垫片Requirejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由requirejs2使用垫片,有一种方法告诉给需要的模块已经加载?

例如:

 <脚本类型=文/ JavaScript的SRC =jquery.js和>< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =undescrore.js>< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =require.js'>< / SCRIPT>
<脚本类型=文/ JavaScript的>
    require.config({
        路径:{
            脊梁:/vendor/js/backbone-min.js
        },
        垫片:{
            骨干:{
                //这些脚本的依赖应该加载之前加载
                //backbone.js
                DEPS:['下划线','jQuery的'],//在这里我想加载已经加载库
            }
        }
    });
< / SCRIPT>


解决方案

好吧,如果下划线已经加载并且可用,你不需要垫片的。骨干网将愉快地加载。如果不是,这可能是因为下划线的的实际加载。

这听起来但错误只是部分使用require.js,你还不如AMD加载它们。要做到这一点,你需要改变你的垫片是这样的:

 垫片:{
    主干:{
        DEPS:[下划线,jQuery的],
        出口:脊梁
    },    强调:{
        出口:_
    }
}

和课程更新路径

Using shim by requirejs2, there is a way to tell to requires that a module is already loaded?

Example:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="undescrore.js"></script>
<script type="text/javascript" src="require.js'"></script>
<script type="text/javascript">
    require.config({
        paths: {
            "backbone": '/vendor/js/backbone-min.js'
        },
        shim: {
            'backbone': {
                //These script dependencies should be loaded before loading
                //backbone.js
                deps: ['underscore', 'jquery'], // here I would like to load the already loaded library
            }
        }
    });
</script>

解决方案

Well, if underscore is already loaded and available, you do not need the shim at all. Backbone will happily load. If not, it's probably because underscore is not actually loaded.

It sounds however wrong to be only partially using require.js, you might as well AMD-load them all. To do that you will need to change your shim like this:

shim: {
    backbone: {
        deps: ["underscore", "jquery"],
        exports: "Backbone"
    },

    underscore: {
        exports: "_"
    }
}

and of course update your paths.

这篇关于使用垫片Requirejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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