Requirejs 我为何要加上一句;垫片;在使用Require.js时是否适用于AngularJS?

Requirejs 我为何要加上一句;垫片;在使用Require.js时是否适用于AngularJS?,requirejs,Requirejs,我已经看到了几个使用此功能的示例: main.js /*global require*/ 'use strict'; require.config({ paths: { angular: './angular', app: './Content/app', ngAnimate: './Scripts/angular-animate', uiRouter: './Scripts/angular-ui-router'

我已经看到了几个使用此功能的示例:

main.js

/*global require*/
'use strict';

require.config({
    paths: {
        angular: './angular',
        app: './Content/app',
        ngAnimate: './Scripts/angular-animate',
        uiRouter: './Scripts/angular-ui-router'
    },
    shim: {
        angular: {
            exports: 'angular'
        }
    }
});

require(['angular', 'app'], function (angular) {
    angular.bootstrap(document, ['app']);
});

有人能给我解释一下为什么需要垫片吗?我的应用程序使用其他模块,如angular ui router、jQuery等。我是否需要做类似的事情并为这些模块添加一个垫片?

规则非常简单:如果库/脚本/包/插件支持AMD,那么您不需要垫片。(实际上,不能为其使用垫片。)如果它不知道,则需要垫片

如果库/etc检测到存在AMD加载程序,并调用
define
使加载程序知道它自己,则该库/etc是AMD感知的

大约从1.8开始,jQuery不需要垫片,因为它调用
define
。另一方面,Angular不调用
define

要知道特定代码段是否需要垫片,可以阅读其文档,或者如果文档对此不清楚,则可以检查源代码以调用
define
。例如,1.11.0具有以下代码:

// Register as a named AMD module, since jQuery can be concatenated with other
// files that may use define, but not via a proper concatenation script that
// understands anonymous AMD modules. A named AMD is safest and most robust
// way to register. Lowercase jquery is used because AMD module names are
// derived from file names, and jQuery is normally delivered in a lowercase
// file name. Do this after creating the global so that if an AMD module wants
// to call noConflict to hide this version of jQuery, it will work.
if ( typeof define === "function" && define.amd ) {
    define( "jquery", [], function() {
        return jQuery;
    });
}
它的外观在不同的情况下会有所不同,但您想要寻找的基本想法是检查
define
是否存在,是否是一个函数,是否设置了
amd
属性,以及调用
define

(请注意,jQuery是一种特殊情况,他们决定在
define
调用中硬编码模块名称(第一个参数:
jQuery
)。通常模块名称不会出现在
define
调用中,而是留给RequireJS根据文件名推断。)