angular_1.default.module uncatch TypeError: 无法读取未定义的属性"module"



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

我通过禁用源代码进一步调查了这个问题,并发现了问题。上面提到的代码编译为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)

我已经用import * as angular from 'angular'替换了import angular from 'angular',现在它工作正常。

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

您也可以

添加

"esModuleInterop": true,

到您的tsconfig.json,而不是在任何地方使用星形导入。

最新更新