AMD(require.js)如何在一个非amd js文件中处理多个类? [英] How does AMD (require.js) handle multiple classes in a single non-amd js file?

查看:94
本文介绍了AMD(require.js)如何在一个非amd js文件中处理多个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来自MootoolsMore库的一堆组件,它们都被编译成一个组件. js文件.有没有一种方法可以在垫片中定义一次库并可以访问其中的所有类?以下方法不起作用,但是我该怎么做?

I'm using a bunch of components from the MootoolsMore library which are all compiled into a single .js file. Is there a way to define the library once in the shim and have access to all the class with in it? The following doesn't work, but how would I do something like this?

shim:{
 mootoolsMore:{
  deps    : ["mootools"]
  exports : ["Slider", "Sortables"]
 }
}

// then inside my module I could access the Slider component like so
define( function(require) {
  var Slider = require('mootoolsMore').Slider

推荐答案

不需要仅用于RequireJS的填充"功能.您可以只使用标准的AMD加载器API:

There is no need for "shim" functionality that is specific to RequireJS only. You can just use standard AMD loader API:

require(['js!path/to/MooToolsCore.js'], function(){
    // nesting in order to insure that Moo Core runs first
    require(['js!path/to/MooToolsMore.js'], function(){
        // Your Slider and Sortables will be in global.
        // just use them.
        window.Slider(/* ... */)
    })
})

请注意,RequireJS不需要"js!"插件需要显式声明,但只能查看文件的扩展名.如果是".js",它将通过"js"插件运行文件.这是非标准行为(与AMD规范中不同),但是在RequireJS上,您应该可以替换以下行:

Note, RequireJS does not need "js!" plugin to be declared explicitly, but, instead, just looks at the extension of the file. If it's ".js" it runs the file through "js" plugin. This is NON-standard behavior (as in not in AMD spec), but on RequireJS you should be able to replace line like:

['js!path/to/MooToolsMore.js']

与此:

['path/to/MooToolsMore.js']

这篇关于AMD(require.js)如何在一个非amd js文件中处理多个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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