4

My app is really big and has like 30 components and pages, I load all of them in my app.module.ts and sometimes the application turn slow. I wonder if it has anything to do it.

My question: What's the correct way to lazy load components and use angular 2 features (more modules) with Ionic 2?

2

1 Answer 1

1

Since Ionic 3, you can lazy load components.

Simply, create a new module for each component/page.

Here's an example of how a module of the HomePage should look like:

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
@NgModule({
  declarations: [MyApp, HomePage],
  imports: [ ... ],
  bootstrap: [IonicApp],
  entryComponents: [MyApp, HomePage],
  providers: [ ... ]
})
export class AppModule {}

After creating the module, attach @IonicPage() to the component:

import { Component } from '@angular/core';
import { IonicPage } from 'ionic-angular';
@IonicPage()
@Component(... )
export class HomePage { ... }

Now you can use your page/component as a string without using the import statement:

rootPage:any = 'HomePage';

for more descriptibe answer, check out this Ionic Lazy Loading blog post.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.