简体   繁体   English

使用带角度的requireJS; 无法读取未定义的属性“模块”

[英]Using requireJS with angular; Cannot read property 'module' of undefined

Ok, there is a bunch of questions with similar titles, but none of them helped me solve my problem so here it is. 好吧,有一堆问题有类似的标题,但没有一个帮助我解决我的问题,所以在这里。

I am trying to use Angular with RequireJS; 我正在尝试将Angular与RequireJS一起使用; I am getting the error message Uncaught TypeError: Cannot read property 'module' of undefined which is referring to the angular object within the angular-routes.js file . 我收到错误消息Uncaught TypeError: Cannot read property 'module' of undefined它指的是angular-routes.js fileangular对象。 This suggests to me that requireJS is trying to load angular-routes before angular has loaded. 这告诉我,requireJS试图在角度加载之前加载角度路线。 I was under the impression that this wouldn't happen because I have set 'angular-route' as dependant on 'angular' within the shim. 我的印象是,这不会发生,因为我已经将'angular-route'设置为依赖于垫片内的'angular'。 Can anyone spot what I am doing wrong here? 谁能发现我在这里做错了什么?

// Setup requireJS

require.config({

    baseUrl : "scripts",

    packages : [
        { "name":"angular", "location":"../bower_components/angular", "main":"angular"},
        { "name":"angular-route", "location":"../bower_components/angular-route", "main":"angular-route"},
        { "name":"angular-animate", "location":"../bower_components/angular-animate", "main":"angular-animate"},
        { "name":"angular-storage", "location":"../bower_components/angular-storage/dist", "main":"angular-storage"},
        { "name":"jquery", "location":"../bower_components/jquery/dist", "main":"jquery"}
],

   shim:{
        'angular' : { exports : 'angular', deps : ['jquery'] },
        'angular-route' : { deps : ['angular'] },
        'angular-animate' : { deps : ['angular'] },
        'angular-storage' : { deps : ['angular'] },
    }

});

// Load app files
function loadApp($, app)
{
    angular.element(document).ready(function(){
        angular.bootstrap(document, ['mealPlannerApp']);    
    });
}
requirejs(['app'], loadApp);

My app.js file is as follows 我的app.js文件如下

(function() {

    // Declare AMD module with dependencies
    define(['angular', 'angular-route', 'routes'],

    function(config)
    {

        var app = angular.module('mealPlannerApp', ['ngRoute', 'ngAnimate', 'ngAnimate']);
        app.config(config);
    });

}());

Packages are great for your internal packages, I haven't tried to load main AngularJS files as them. 软件包非常适合您的内部软件包,我没有尝试加载主要的AngularJS文件。

Please read http://jonathancreamer.com/require-js-packages-for-building-large-scale-angular-applications/ for some better clarification and examples than change config section to something like this. 请阅读http://jonathancreamer.com/require-js-packages-for-building-large-scale-angular-applications/以获得更好的说明和示例,而不是将配置部分更改为类似的内容。 Major change is replacement of packages with paths 主要变化是用路径替换

Please also consider to change variables to camelCase style 还请考虑将变量更改为camelCase样式

require.config({
    paths: {
        angular: '../bower_components/angular/angular',
        'angular-route': '../bower_components/angular-route/angular-route',
        'angular-animate': '../bower_components/angular-route/angular-animate',
        'angular-storage': '../bower_components/angular-route/angular-storage'
    },

    shim: {
        'angular' : { exports : 'angular', deps : ['jquery'] },
        'angular-route' : { deps : ['angular'] },
        'angular-animate' : { deps : ['angular'] },
        'angular-storage' : { deps : ['angular'] },
    },

    priority: [
        "angular"
    ]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM