App module not work with providers

Yet another feature that doesn’t really work as advertised. By the time things are transpiled to ES5, access control vanishes. TS will enforce it, and theoretically you aren’t supposed to be able to access private properties from within templates (although I believe ionic-app-scripts subverts that as well).

So I follow the convention of putting leading underscores on private properties (which I broke in that previous post) and not accessing those from outside the defining class.

As I’m sure you’re aware, there is a significant difference between having an access qualifier and not having one at all: if there is an access qualifier, TS will automatically define an object property for you and initialize it. If not, it won’t. So I only use access qualifiers on constructor parameters that will be used elsewhere; if they will only be used inside the constructor itself, I leave them off.

1 Like

In OOP you have these rules for modifiers:

  • Public: the member is accessed on any object;
  • Private: the member is only accessed internally;
  • Protected: the member is only accessed in the class where it is declared or classes of children, who inherits this class;
  • internal: the member can be only accessed by classes objects that are same assembly of the class it was declared.

The origin of my doubt is this tutorial:

In the explanation, the code of this page dont has declared the [provider]: providerName in @components of the page.ts, and i has understood that if declare in app.module, the providers would become global.

But, now, i understand thar i ALWAYS have to import the provider and declare in @component with:

@Component({

providers: [TimelineProvider],

})

My understand is correctly?

No. Each provider gets the scope of where it is declared. If you want it to be shared amongst all consumers, declare it only in the app module. 99% of the time, this is the case. In the rare situation where you want a separate provider instance for each consumer, declare it in that consumer’s providers.

What version of the Ionic framework are you on @wesley_b2 ? This tutorial is for RC0, since RC0 Ionic has come a long way. As did angular. As did the way you have to declare your providers (only at app.module level in most cases, as @rapropos explained) and how to import them. Perhaps you should take a look at newer code examples from Josh Morony. He has written some excellent tutorials which you can find over here : https://www.joshmorony.com/tag/ionic2/