@Xiwi
If we have a “page” (without @IonicPage()
and without route) that we are going to use just once on our app, should it be placed inside pages or components? I see components as parts of our app that we need on different places.
Ionic pages are just components, but they have some specific behaviour, like having ion-content
in it and receiving parameters with navParams
(instead of @Input
) and being created with NavController
.
If your component doesn’t behave like that, I advise you to consider it as a simple component, not a page.
I added entryComponents
on all modules
to have exactly the same configuration. Why some modules
don’t have this?
Using entryComponents
allow you to create components dynamically, either with ComponentFactoryResolver explicitly or under the hood.
For example, when you do this.navCtrl.push(MyComponent)
you instantiate the component through its reference, instead of including it in the template like <my-component></my-component>
, so you need to include it in entryComponents
, otherwise you receive an error.
If you use the component only in the template (HTML) you don’t need to include it in entryComponents
.
But when creating a module per component, I include it in entryComponents
as a best practice, because I don’t want to assume the way the component consumers will create it, so it will work in both cases (defining it in the template or creating it by reference).
I don’t know why on previous link those components work without exports
and entryComponents
.
If the 2 components A
and B
are declared in the same module, then you don’t need to export B
to be used by A
(because both are in the same module). If a component C
in another module uses B
, then you need to export B
.
When you use one module per component, you should export the component always (because its consumers will all be in other modules).
About entryComponents
, you don’t need it every time, but I recommend as a best practice (see what I said before, for more details).
Just one thing, what about shared components as translations or other providers? Do we need to keep them on app.module
or we need change every single component?
I don’t have a definition of shared components (by itself). When one component is used by several others, I just include its module in the modules of the components that use it, just like with any other component.
I let the task of reusing or not this component code (in the generated files) with the module bundler (webpack in this case, although the way it’s setup in Ionic3 don’t make it very efficient in some cases). No complexities here. I don’t store shared data in components, tough, but in providers.
About providers, I’m incuding all of them in AppModule
, while I’m still in Ionic3
. When I migrate to Ionic4
, I will be using Angular6
and define the providers with @Injectable({ providedIn: 'root' })
. So I won’t need to import the provider in any module, it will be only one in the application, and will have the benefit of lazy loading (for now, it doesn’t have it because I include all of them in AppModule
, so all of them are eager loaded, unfortunately).