Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public or private - Angular 2 component class methods confusion

I struggle to understand which methods should be private and which should be public in component class.

It seems to be fairly easy in service to judge if method is public or private e.g:

export class MyServiceClass {
  private _cache = {}; // this value is private and shouln't be accessed from outside
  public accessCache(){ // it's public as it's an API method
    return this._cache;
  }
  public setCache(newVal){
     this._cache = newVal;
  }
}

Following that logic all methods in component should be private because none of the methods should be exposed outside of the class. (according to to that post component and its view are one entity)

export class MyComponent {      
  private _getRandomNumbers(){ // this is used in view only
    /*..*/
  }
}

There is no tragedy but then in this video you can learn that only public methods of component should be unit tested. By following above I can't find any reason to have public methods in component class but I still have some methods which are worth testing (especially methods used in view). It means that I'm completely lost in the meaning of private and public methods in angular world.

So my question is simple:

which methods in components should be marked as public and private.

like image 991
LJ Wadowski Avatar asked Jun 22 '16 13:06

LJ Wadowski


People also ask

What is difference between public and private in angular?

1. Public: All the properties and methods could be accessed everywhere if they are declared as public. 2. Private: The private declared properties and methods can be accessed only within the class definition itself.

What access modifier do we use for methods that are internal to a class and should not be visible from the outside angular?

Private. The private access modifier cannot be accessible outside of its containing class. It ensures that the class members are visible only to that class in which it is containing.

Why we use private constructor in angular?

It is about encapsulation, and when you have a field or method on your component that you want to encapsulate in it, making it clear that it shouldn't be accessed from anywhere else, then you should absolutely make it private : That's what private is for: It signals your intent that whatever you've put it on shouldn't ...

Can private variables be accessed by HTML angular?

A Question came up on StackOverflow about accessing private variables on an Angular Component class inside an Angular template. How do you do it? The short answer is you can't.


1 Answers

In Component Class, I would say, set everything as public (if no access modifier, it's public by default).

In normal case, we don't extends a component class, therefore, access modifier is not needed, IMHO.

There are cases where we will inherit a component. See here component inheritance in Angular 2. However, even in these cases, access modifier might not be necessary.

...

export class MyComponent {
  // injected service as private
  constructor(private _randomSvc: RandomService) {} 

  getRandomNumbers(){ } // leave it as public

  @Input()
  myInput: string; // leave it as public

  @Output()
  myOutput; // leave it as public
}

Remember Javascript itself has NO access modifier. The access modifier only applicable during development(IDE). While modifier is useful in some cases, I would suggest minimise the usage of that.

like image 169
Chybie Avatar answered Oct 19 '22 02:10

Chybie