1

我正在尝试使用 gulp 和 bower 将需要新的第三方模块的功能添加到 Angular 应用程序中。在开发模式下一切都很好,但我添加的任何新模块都会"Failed to instantiate module"在生产中产生臭名昭著的错误。我尝试禁用 uglify 构建步骤,但没有任何区别。只需将新模块声明为我自己的一个模块的依赖项就足以产生错误。

与许多这些问题不同,我已经在使用字符串数组语法来处理依赖项,并且该应用程序已经与各种其他模块一起运行。我发现我添加的内容与已在使用的其他库之间没有区别。

这是我目前在 include 方面的尝试ngCookies,但我对其他人也做过同样的事情,例如。ngFileUpload结果相同。

bower.json:

  "dependencies": {
    "jquery": "^2.1.4",
    "angular": "^1.5.9",
    "angular-sanitize": "^1.4.5",
    "extras.angular.plus": "^0.9.2",
    "moment": "^2.10.3",
    "angular-ui-router": "^0.2.15",
    ...
    "angular-cookies": "^1.5.9", // new
  },

索引.html:

<!-- build:js js/lib.js -->
<!-- bower:js -->
<script src="/bower_components/jquery/dist/jquery.js"></script>
<script src="/bower_components/angular/angular.js"></script>
<script src="/bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="/bower_components/extras.angular.plus/ngplus-overlay.js"></script>
<script src="/bower_components/moment/moment.js"></script>
<script src="/bower_components/angular-ui-router/release/angular-ui-router.js"></script>
...
<script src="/bower_components/angular-cookies/angular-cookies.js"></script> // new
<!-- endbower -->
<!-- endbuild -->

核心模块声明:

(function () {
  'use strict';

  angular
    .module('app.core', [
      'ngAnimate',
      'ngSanitize',
      'blocks.exception',
      'blocks.logger',
      'blocks.router',
      'ui.router',
      'firebase',
      'ngMap',
      'ngplus',
      'ngCookies', // new
    ]);

})();

缩小/丑化的 js 输出:

应用程序.js

function(){"use strict";angular.module("app.core",["ngAnimate","ngSanitize","blocks.exception","blocks.logger","blocks.router","ui.router","firebase","ngMap","ngplus","ngCookies"])}(), ...

lib.js

t.module("ngCookies",["ng"]).provider("$cookies", ... 

控制台输出:

Error: [$injector:modulerr] Failed to instantiate module app due to:
[$injector:modulerr] Failed to instantiate module app.core due to:
[$injector:modulerr] Failed to instantiate module ngCookies due to:
[$injector:nomod] Module 'ngCookies' 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.5.9/$injector/nomod?p0=ngCookies
4

1 回答 1

1

正如我所看到的,您将不同版本的 Angular API 加载并绑定在一起。理想情况下,您应该参考具有相同版本的 Angular API 库。但在你的情况下,你有angular-animate哪个1.4.8版本,但其他角度 API 有1.5.8版本。这可能会在内部发生冲突。

相反,我建议您将所有 Angular API 版本都与 1.5.8 相同。这可以解决你的问题。

于 2016-12-07T20:53:36.053 回答