Stephan K.
Stephan K.

Reputation: 15702

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

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?

Upvotes: 8

Views: 4607

Answers (1)

Mathieu Nls
Mathieu Nls

Reputation: 2365

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.

Upvotes: 14

Related Questions