BeetleJuice BeetleJuice - 1 year ago 101
TypeScript Question

Angular 2 Ahead-of-Time compiler: must I make all class properties public?

Angular 2 rc 6
typescript 2
node 4.5.0
npm 2.15.9
Windows 7

I'm trying to move from Just-in-Time to Ahead-of-Time compilation and I'm relying on these resources:

I understand that I need to run the compiler
to generate
files, and that I need to change
to use
instead of
to bootstrap. I've hit a roadblock though and don't know how to proceed.

1. I've confirmed that the App runs without errors using Just-in-Time compilation. My


2. I clear all my app files from my production folder, but keep 3rd party libraries (eg: Angular 2, Angular 2 Material)

3. I run
"node_modules/.bin/ngc" -p ./
This runs with no output to the console. I see an
file for each of my
components and modules. I also see a
file for each of my
that held component styles. In addition,
files have been transpiled and placed in the production directory

4. If I try to run the app at this point, I see
404 not found
errors for all the
files that held component templates

5. I manually move all template files (
) to production dir and run the App. It runs fine, but it still uses Just-in-Time compilation (255 requests, including

6. I change my


On its own, this makes no difference since the new code has not been compiled. However, I don't know what to do from here.

Should I run
again? If so I get lots of errors of the type:

Error at C:/path/to/notify.component.ngfactory.ts:113:41: Property 'visible' is private and only accessible within class 'NotifyComponent'
... (many more like that with lots of properties from lots of components)
Compilation failed

Does using AoT compilation mean that I must make all my class properties public? Am I missing a step?

Answer Source

For a given component all its members (methods, properties) accessed by its template must be public in the ahead-of-time compilation scenario. This is due to the fact that a template is turned into a TS class. A generated class and a component are 2 separate classes now and you can't access private members cross-class.

In short: you can't access private members in your templates if you want to use ahead-of-time compilation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download