通过RequireJS从CDN加载角没有注入加载、RequireJS、CDN

2023-09-13 05:14:13 作者:满是遗憾

在我的项目,我想使用RequireJS和引导我的应用程序如下:

In my project I want to use RequireJS and bootstrap my app as follows:

requirejs.config({
baseUrl: 'scripts/vendor',
paths: {
    jquery: [
        'https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min',
        'jquery'
    ],
    angular: [
        'http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min',
        'angular'
    ],
    app: '../app'
}
});

require(['require', 'jquery', 'angular', 'app'], function(require, $, angular, app) {
console.log(require);
console.log($);
console.log(angular);
console.log(app);
});

在我的index.html只有RequireJS通过脚本标签,其中RequireJS加载上面code加载。什么工作: - 在我的网络监控我可以看到RequireJS,jQuery的,角度和应用程序加载是 - 该消息的console.log打印的要求正确,jQuery和应用

On my index.html only RequireJS is loaded via script tag, where the RequireJS loads the above code. What works: - in my Network monitor I can see that RequireJS, jQuery, Angular and app are loaded - The console.log messages print correct for require, jQuery and app

角的对象是某种不确定的。但是,如果我不从CDN加载它,并用我的本地负载,它的作品!本地文件是一个RequireJS包装,看起来像这样:

The angular object is somehow undefined. But if I don't load it from CDN and use my local load, it works! The local file is a RequireJS wrapper that looks like this:

define(['/components/angular/angular.min.js'], function () {
    return angular;
});

我如何与角的CDN这项工作?还是这取决于支持角?

How do I get this work with Angular'S CDN? Or does this depend on support from Angular?

推荐答案

首先,你是混乱的路径与垫片

First, you are confusing "paths" with "shim"

路径是好的,不要去垫片的行为。但是,你需要让你的路径正确的:

Path is good, don't go for "shim" behavior. But, you need to make your "paths" proper:

paths: {
    jquery: 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min',
    // NOTE: angular is "plain JS" file
    angular: 'http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min',
    app: '../app'
}

然后,你需要让的需要去有东西归还给你了...只是使用武力,卢克:),并希望当你需要他们正确的全局在那里:

Then, you need to let go of the need to have something returned to you... Just "use the force, Luke" :) and expect the right globals to be there when you need them:

require(['jquery', 'app', 'angular'], function($, app, thisValueDoesNotMatter) {
    // you don't need to wrap "require" Just use global
    console.log(require);
    console.log($);
    console.log(app);

    // note, angular is loaded as "plain JavaScript" - not an AMD module.
    // it's ok. It returns "undefined" but we just don't care about its return value
    // just use global version of angular, which will be loaded by this time.
    // because you mentioned it in your dependencies list.
    console.log(window.angular);
});
 
精彩推荐
图片推荐