Property 'X' is private and only accessible within class 'xyzComponent'

AngularTypescriptTypescript TypingsAngular2 Aot

Angular Problem Overview


I'm trying to build angular2 application for production for that I'm following this blog. After my ngc successful compilation when the tsc compilation takes place it generates below error shown in the image:

enter image description here

After searching for a while I found this blog which explains the problem in "The context property" section which I'm not able to understood properly may be it give some good idea to you that what's happening wrong. basically when we're making a variable private we're getting "ERROR: Property is private and only accessible within class". I'm not understanding why is it coming.

Kindly help us as we're banging our head in this problem for past couple of days.

Angular Solutions


Solution 1 - Angular

For a given component all its members (methods, properties) accessed by its template must be public in the AOT 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.

For better explaination https://github.com/angular/angular/issues/11422

Solution 2 - Angular

Maybe another even simpler answer is:

> Guys, please don't call private methods, fields or properties from the HTML :)


P.S. when compiling the *.ts code to *.js, AOT refuse to connect non-public members with the HTML template.
> And "yes" this will make your build pipeline to fail :D

Solution 3 - Angular

I got this when I declared private injectables in the constructor:

constructor(private service: SpecificObjectService) { }

And used them in the template:

*ngFor="let pd of service.listSpecificObject "

The solution is:

constructor(public service: SpecificObjectService) { }

Solution 4 - Angular

So I fixed this problem I'll keep this short and simple. To fix this I read this blog deeply. As in section "The context property" The solution for this problem is that Don't use or create a private variable if you want to use it in the view directly when your are creating your build with AOT (i.e., Ahead Of Time) for production.

***for Example ***

// component.ts
@Component({
  selector: 'third-party',
  template: `
    {{ _initials }}
  `
})
class ThirdPartyComponent {
  private _initials: string;
  private _name: string;

  @Input()
  set name(name: string) {
    if (name) {
      this._initials = name.split(' ').map(n => n[0]).join('. ') + '.';
      this._name = name;
    }
  }
}

output: Property '_initials' is private and only accessible within class 'ThirdPartyComponent'.

Solution:

update this private _initials: string; to simply _initials: string;

For this answer Harish Gadiya provide me some help so thanx for that.

Solution 5 - Angular

This works for me guys: Simply change service to public.

constructor(public service: SpecificObjectService) { }

App working in production!!

Solution 6 - Angular

If you want to use router in view, please make it public.

E.g:

<button 
   [routerLink]="['/login']"
   [queryParams]="{redirectTo: router.url}"
   translate="Please sign in to use this feature"
/>
import { Router } from '@angular/router'; 

constructor(
   public router: Router; // don't make it private
) {}

I overlooked it until Github CI sends me a warning mail.

Solution 7 - Angular

ok see this is really a simple javascript es6 problem, if you must keep the data type private you can simply do this

privateAccess(){
     return this.cannotAccessByInstanceButStillNeeded
}

Solution 8 - Angular

You can always add a // @ts-ignore before the lines causing this error. Check Doc

Solution 9 - Angular

Just remove the 'private' access modifier in front of the variable. If it is the instance declared in a constructor then just change 'private' to 'public'.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSumit KhanduriView Question on Stackoverflow
Solution 1 - Angularharish gadiyaView Answer on Stackoverflow
Solution 2 - AngularArsen KhachaturyanView Answer on Stackoverflow
Solution 3 - AngularTiyebMView Answer on Stackoverflow
Solution 4 - AngularSumit KhanduriView Answer on Stackoverflow
Solution 5 - AngularCarlos ValdesView Answer on Stackoverflow
Solution 6 - Angularglinda93View Answer on Stackoverflow
Solution 7 - AngularMIchael OdumosuView Answer on Stackoverflow
Solution 8 - AngularMelchiaView Answer on Stackoverflow
Solution 9 - Angularvaibhavi dhopateView Answer on Stackoverflow