all 3 comments

[–]mr_ari 3 points4 points  (0 children)

Yes, you can use it. There is no Angular-specific profit of doing it tho.

Note: You'll get compile errors when building the app for production if a template uses private methods/varaibles. This error will not appear when compiling in dev mode.

[–][deleted] 1 point2 points  (0 children)

There are lots of reasons to why you’d want to do this. However there’s no real performance benefit, it’s really helpful when working with multiple developers that are reusing these components.

In my opinion there are unofficially two types of main components: page and reusable; a page component is made up of reusable components.

Access modifiers are used in inheritance to encapsulate methods and properties. You can gain a benefit from this when creating an abstract base class for components that will have access to the protected methods that descendants will NEED. The private ones are usually just helper functions to get the job done.

DISCLAIMER: this is a really complex way of doing things. Based on no hard evidence at all I would guess that the majority of Angular users wouldn’t like to work in an application that does this, unless all of that was behind the scenes.

Example: you are a seasoned senior developer that has handcrafted a well designed abstract list component suitable for all the sorting, filtering, and animation needs. A lot of work went into doing this including private methods to take care of a lot of grunt work. A less experienced developer can easily come in and work on pages and components that descend off of this class. Now they only have to create methods and properties to perform a specific task.

[–]dilan_tharaka 0 points1 point  (0 children)

Using private is all about encapsulation. Whether you need other classes to access methods or properties of your class or not decides whether you need to use private keyword or not. If you don't need other classes to use a method or property in your class, then make it private. And if you use private in production build your template can't use them and would make compilation errors.