Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lazy load module in matDialog

I have a component which is part of a lazy load module.

Is there a way to matDialog.open() and lazy load the module and show the component?

export class testComponent implements OnInit {
  constructor(
    public matDialog: MatDialog,
    private moduleLoader: NgModuleFactoryLoader
  ) {}
  ngOnInit() {}

  openModal() {
    this.moduleLoader
      .load("./modules/test-modal/test-modal.module#TestModalModule")
      .then((module: NgModuleFactory<any>) => {
        this.matDialog.open(/*insert component and load the module*/);
      });
  }
}
like image 214
nicker Avatar asked Feb 25 '19 05:02

nicker


People also ask

What is lazy loading of modules?

Lazy loading is an approach to limit the modules that are loaded to the ones that the user currently needs. This can improve your application's performance and reduce the initial bundle size. By default, Angular uses eager loading to load modules.

How does lazy loading work in Angular 6?

Angular 6 allows to Lazy load modules when they are required, they are not loaded at once on application initialization. To implement Lazy loading in Angular 6 we will use the main Routing module which will import all components we want to lazy load.

What is lazy loading components?

Lazy loading is the technique used in optimizing your web and mobile apps, this works by rendering only needed or critical user interface items first, then quietly rendering the non-critical items later. As we build code components the application grows, and the bundle gets very cumbersome in size.

What is lazy loading in UI?

Lazy loading is the practice of loading expensive resources on-demand. This can greatly reduce the initial startup time for single page web applications. Instead of downloading all the application code and resources before the app starts, they are fetched just-in-time, as needed.


Video Answer


1 Answers

I found an example to lazy load module with component in mat-dialog.

Please see refer to:

  • https://medium.com/ngconf/routing-to-angular-material-dialogs-c3fb7231c177

Just in case the link is no longer available, i'd included a brief step and example to do it

1. Create a lazy load module

2. Create entry component(empty component) to launch your modal component

@Component({
template: ''
})
export class DialogEntryComponent {
    constructor(public dialog: MatDialog, private router: Router,
    private route: ActivatedRoute) {
    this.openDialog();
    }
    openDialog(): void {
    const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
        width: '250px'
    });
    dialogRef.afterClosed().subscribe(result => {
        this.router.navigate(['../'], { relativeTo: this.route });
    });
    }
}

3. Create a route for the lazy load module

const routes: any = [
    {
    path: "",
    component: modalComponent(actual component with content)
    }
];
@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
    providers: [DataResolver]
})
export class DialogEntryRoutingModule {}

4. At parent router module, include path to lazy load DialogEntryModule

RouterModule.forRoot([
    {
      path: 'home',
      component: ParentComponent,
      children: [
        {
          path: 'dialog',
          loadChildren:
              "../example/entry-dialog.module#DialogEntryModule"
        }
      ]
    },
    { path: '**', redirectTo: 'home' }
  ])

5. in ParentComponent open the modal by directing to the DialogEntryModule

<button mat-raised-button routerLink="dialog">Pick one</button>
like image 157
nicker Avatar answered Oct 23 '22 20:10

nicker