Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AoT NGC / Angular2: Property is protected and only accessible within class Error

Tags:

angular

ionic2

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?

like image 768
stevek-pro Avatar asked Oct 13 '16 12:10

stevek-pro


People also ask

Is protected and only accessible within the class declaration?

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.

What is AOT mode in Angular?

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.

Is private and only accessible within Class JS?

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.


1 Answers

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.

like image 108
Mathieu Nls Avatar answered Oct 31 '22 03:10

Mathieu Nls