Should I import a component or its module?


#1

I understand that in order to achieve lazy loading, I should import the module, and this is how I load pages.
However, when loading components to be rendered in a page, If I know that they must be rendered, what advantage is there in lazy loading?
Is there a disadvantage in importing the component directly.
The below code shows 2 ways I can import a component, and they both work (importing the module is commented out)

import {NgModule} from '@angular/core';
import {IonicPageModule} from 'ionic-angular';
import {InboxTagsPage} from './inbox-tags';
import {EcFooterComponent} from '../../components/ec-footer/ec-footer'
/* In order to lazy load component in page - import its module.
import {EcFooterComponentModule} from '../../components/ec-footer/ec-footer.module'
 */
@NgModule({
  declarations: [
    EcFooterComponent, InboxTagsPage
  ],
  imports: [// EcFooterComponentModule,
    IonicPageModule.forChild(InboxTagsPage)],
  providers: [],
  exports: [InboxTagsPage]
})
export class InboxTagsPageModule {};

#2

I don’t believe there’s any functional difference between the two. You are presumably aware that either way, you are getting duplicate copies of all the EcFooterComponent code in every page that uses it.


#3

Thanks!
I was just wondering about the app size issue, regarding pipes.
I created a custom pipe using “ionic g pipe”, which imports the pipe at the app.module level. However, this doesn’t work, and i need to import the pipe at the page.module level.
If I want it imported at the app.module level, I need to manually create a module for the pipe (Ionic does not do this by default), and then import the PipeModule into app.module.
Would this help reduce the app size? If so, why doesn’t Ionic create a PipeModule file by default?


#4

Also, should I import all my compoennt.module files at the app level? Is it a trade off between reducing app size and lazy loading?
I would still lazy load pages.


#5

I would suggest building it both ways and examining the results. Things may have changed since I investigated the situation and decided to completely avoid lazy loading altogether, and I can’t say with confidence that that’s the best course of action for your situation.