Get `Cannot read property 'type' of null`

2019-09-11 00:40发布

问题:

Update to Angular2 2.0.0-rc.5, run in browser without any warning, but when try AOT compile with ngc -p command, get the flowing error:

Here is my project https://github.com/beginor/learning-angular2

回答1:

Do not use default exports in your code:

// somefile.ts
export default function (...) {
    ...
}
...
// some-other-file.ts
import whatevar from './somefile';

use explicit names instead

// somefile.ts
export function whatevar(...) {
    ...
}
...
// some-other-file.ts
import { whatevar } from './somefile';

AOT is incompatible with default exports (among other things). But unlike other incompatibilities, this one generates the most cryptic error message ever.



回答2:

All these errors are related to AoT. This blog post explains the changes to be made in your code.

Making your Angular 2 library statically analyzable for AoT

  1. const lambda => export function
  2. default export => named export
  3. private, protected accessors should be changed to public for any members accessed from template
  4. dynamic component template => static template
  5. moduleId should be set on components with templateUrl


标签: angular