【问题标题】:angular_1.default.module Uncaught TypeError: Cannot read property 'module' of undefinedangular_1.default.module Uncaught TypeError:无法读取未定义的属性“模块”
【发布时间】:2017-05-17 10:26:22
【问题描述】:

我正在使用 Angular 1.6、webpack、gulp 和 typescript 创建一个入门模板。我能够编译和构建我的应用程序,但应用程序没有引导。这个问题很奇怪,因为当我调试 angular.module 时已定义并且可以从控制台 angular.module('testModule', []) 创建模块,但作为恢复调试器,我在 core.module.ts export const CoreModule = angular.module('app.core', []).name; 处遇到异常。

我通过禁用源代码进一步调查了这个问题,我发现了这个问题。上述代码编译为exports.CoreModule = angular_1.default.module('app.core', []).name;,其中angular_1 上没有名为default 的属性。 代码参考https://github.com/muditsaurabh/awesome-ng-starter

Uncaught TypeError: Cannot read property 'module' of undefined
    at Object.<anonymous> (app.bundle.js?cd4d443…:1)
    at e (vendor.bundle.js?cd4d443…:1)
    at Object.<anonymous> (app.bundle.js?cd4d443…:1)
    at e (vendor.bundle.js?cd4d443…:1)
    at Object.<anonymous> (app.bundle.js?cd4d443…:1)
    at e (vendor.bundle.js?cd4d443…:1)
    at window.webpackJsonp (vendor.bundle.js?cd4d443…:1)
    at app.bundle.js?cd4d443…:1
vendor.bundle.js?cd4d443…:6Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:nomod] Module 'app' 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.6.1/$injector/nomod?p0=app
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:377
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11230
    at n (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:10668)
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11004
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20796
    at r (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:809)
    at h (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20644)
    at le (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:22631)
    at a (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8560)
    at st (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8870)
http://errors.angularjs.org/1.6.1/$injector/modulerr?p0=app&p1=Error%3A%20%…2F%2Flocalhost%3A8000%2Fvendor.bundle.js%3Fcd4d4432605c6f5be56a%3A6%3A8870)
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:377
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11230
    at n (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:10668)
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:11004
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20796
    at r (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:809)
    at h (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20644)
    at le (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:22631)
    at a (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8560)
    at st (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8870)
http://errors.angularjs.org/1.6.1/$injector/modulerr?p0=app&p1=Error%3A%20%…2F%2Flocalhost%3A8000%2Fvendor.bundle.js%3Fcd4d4432605c6f5be56a%3A6%3A8870)
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:377
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:21073
    at r (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:809)
    at h (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:20644)
    at le (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:22631)
    at a (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8560)
    at st (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8870)
    at at (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:8109)
    at http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:11:13916
    at HTMLDocument.n (http://localhost:8000/vendor.bundle.js?cd4d4432605c6f5be56a:6:17809)

【问题讨论】:

  • 我们需要更多信息,你的 webpack.config 和入口文件是什么样的?
  • @Dieterg 我已添加 git repo 链接请克隆它以查看代码
  • @Dieterg 我通过禁用源映射进一步调查了这个问题,我发现了问题。上述代码编译为exports.CoreModule = angular_1.default.module('app.core', []).name; angular_1 上没有名为 default 的属性

标签: angularjs typescript gulp webpack


【解决方案1】:

我已将import angular from 'angular' 替换为import * as angular from 'angular',现在它工作正常。

【讨论】:

  • 但是为什么这是必要的?我在node_modules/angular 中看到一个 index.js 文件,它将默认导出设置为 'angular' (this v 1.6)
  • 在 webpack 2.X 中将 JS 更改为 TS 的魔术修复。
【解决方案2】:

您确定 AngularJS 在您的脚本之前包含吗?该错误意味着Javascript尚未看到对象角度,因此无法在其上调用模块。

【讨论】:

  • 我能够从控制台获取角度对象,但是当我跨过export const CoreModule = angular.module('app.core', []).name; 行时,我得到了异常。我认为这个错误具有误导性。
【解决方案3】:

你也可以添加

"esModuleInterop": true,

到您的tsconfig.json,而不是到处使用星号导入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 2021-04-28
    • 2017-07-20
    • 1970-01-01
    • 2020-03-28
    • 2023-04-11
    • 2016-04-22
    相关资源
    最近更新 更多