小编典典

如何解决构建的 AngularJS 应用程序的依赖问题?

all

我是 AngularJS 的新手,正在创建一个将使用 Grunt 构建的应用程序。

当我构建和运行我的应用程序时,我注意到一些与依赖加载顺序相关的问题:

Uncaught Error: [$injector:nomod] Module 'mycompany.admin.forms' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.2.13/$injector/nomod?p0=mycompany.admin.forms 

在构建的应用程序文件中,模块在声明之前正在使用:

angular.module('mycompany.admin.forms').controller('editController', ['$scope', function($scope) {
    // ...
}]);

angular.module('mycompany.admin.forms', [])

  .config(['$routeProvider', function($routeProvider) {
    // ...
  }]);

以下是我项目的 gruntfile.js 中的相关片段:

grunt.initConfig({
    distdir: 'build',
    pkg: grunt.file.readJSON('package.json'),
    src: {
        js: ['src/**/*.js'],
    },
    concat: {
        dist: {
            src: ['<%= src.js %>', '<%= src.jsTpl %>'],
            dest: '<%= distdir %>/admin/app.js'
        }
    }
    ...
});

我知道我可以通过将给定模块的所有源代码放入一个文件中来解决这个问题;但是,我想尝试将每个事物(每个控制器、每个服务等)保存在自己的文件中。

我该如何解决这个依赖加载顺序问题?


阅读 153

收藏
2022-07-13

共1个答案

小编典典

grunt 连接文件的顺序是什么?看起来您只是在读取一个目录,并且如果模块声明所在的 javascript 文件位于控制器所在的文件之后,则连接的文件将以错误的顺序构建。

可能最简单的解决方案是明确列出包含模块声明的文件(使用 [])作为源数组中的第一个文件。

2022-07-13