How to debug angular 2 aot failures

2020-03-19 03:14发布

问题:

I have an angular 2 app generated by ng cli.

- When I run ng build (or) ng build --prod --aot=false and serve up the page things just work fine.
- But when I try to enable aot by running ng serve --aot=true and serve up, the page breaks with multiple DI errors like below

Very hard to debug. Any idea on how to debug these issues?

EXCEPTION: No provider for Options!
error_handler.js:59 ORIGINAL STACKTRACE:
ErrorHandler.handleError @ error_handler.js:59
(anonymous) @ application_ref.js:272
webpackJsonp.679.ZoneDelegate.invoke @ zone.js:229
onInvoke @ ng_zone.js:271
webpackJsonp.679.ZoneDelegate.invoke @ zone.js:228
webpackJsonp.679.Zone.run @ zone.js:113
(anonymous) @ zone.js:509
webpackJsonp.679.ZoneDelegate.invokeTask @ zone.js:262
onInvokeTask @ ng_zone.js:262
webpackJsonp.679.ZoneDelegate.invokeTask @ zone.js:261
webpackJsonp.679.Zone.runTask @ zone.js:151
drainMicroTaskQueue @ zone.js:405

回答1:

Are you by any chance using: angular2-logger? I've had the exact same error and found out even though I don't actually use the Options I had to provide them for --aot to work.

Eg: In your AppModule have to following import:

import {Logger, Options} from "angular2-logger/core";

Then in you list of providers make sure you add Options:

providers: [
  Logger,
  Options // <-- this is key
]

That made the sure AOT included "Options".

Now, how did I find out? The error gives you the hint it cant find "Options". So I used the Developer Tools to look at the generated sources using the sourceMaps (I looked at main.bundle.js). There I searched for the string "Options" and the only hit gave me also the final hint:

__WEBPACK_IMPORTED_MODULE_9_angular2_logger_core__["Options"])

Now thate made me realize after looking at the docs I had to provide the Options as well.

Hope this helps. :)