Importing dependencies in constructor, best practices? [Ionic 2]


#1

I finally took the leap of faith towards Ionic 2, however I am a bit confused in one part. While browsing tutorials, many of them inject providers into the constructor using the “public” way. Example: constructor(public someService: SomeService)

However, the default template injects the NavController provider as a private. I am wondering what is the best practice for injecting dependencies into my constructor and what exactly public and private does within injecting.

Thanks!


#2

It’s only syntax encapsulation. So you could use like you want. I create private.


#3

Thank you! What is the benefits of using public vs private or vise versa?


#4

I don’t think either private or public could actually matter in a practical sense.

I think It’s mostly for Typescript convenience, when you inject an object with private, if code outside of current component tries to do thatcomponent.privateObject your IDE should show a visual complain or sth.