In on of our components in our Angular 2 / Ionic 2 (final/rc0) project I am using:
protected contentTarget: ViewContainerRef;
ngOnInit() {
this.contentTarget.createComponent(componentFactory);
}
AoT Compiler says:
Error at ....: Property 'contentTarget' is protected and only accessible within class 'IncludeTemplateComponent' and its subclasses.
Variable (property) is not used anywhere else in whole project.
So... could anybody shed some light on this, is createComponent factory passing the contentTarget variable to its child or why does compiler does not like protected here? Are protected variables "forbidden" in all of Angular2 now?
The error "Property is private and only accessible within class" occurs when we try to access a private property outside of a class. To solve the error, declare the class property as public if you need to access it from outside the class.
The Angular ahead-of-time (AOT) compiler converts your Angular HTML and TypeScript code into efficient JavaScript code during the build phase before the browser downloads and runs that code. Compiling your application during the build process provides a faster rendering in the browser.
Like their public equivalent, private static methods are called on the class itself, not instances of the class. Like private static fields, they are only accessible from inside the class declaration.
From here
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.
My take is that contentTarget
is used by the template engine and thus, must be public for AOT to work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With