1

I'm using angular to create an ui library. In workspace, there is one project for main app, one project for ui library.

In the library which declares modules and routings, I have this code:

parent-routing.module.ts:

import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {ParentComponent} from './parent.component';

const routes: Routes = [
  {
    path: '',
    component: ParentComponent,
    children: [
      {
        path: '',
        loadChildren: '/child/child.module#ChildModule'
      }
    ]
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(routes)
  ],
  exports: [
    RouterModule
  ]
})
export class ParentRoutingModule {
}

In app.module.ts, I declare the ParentModule as below:

const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('ui-lib').then(m => m.ParentModule)
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

When I tried compiling my app, the console throws me an error:

ERROR in ./src/app/app-routing.module.ts Module not found: Error: Can't resolve './../../../../dist/ui-lib/modules/parent/parent.module.d.ngfactory' in '/Users/linhnguyen/nested-module-playground/projects/main-app/src/app'

After searching on Google, I found this comment. The commenter mentioned about creating a module and import every component to the route. That can resolve the issue of loading module in library, but it is not lazy loaded.

Here is my workspace on stackblitz

Does anyone have any solutions please ?

Thanks,


UPDATE

In my main application, I declared the wrapper modules for parent & child component as below:

  • child-wrapper.module.ts

const routes: Routes = [
  {
    path: '',
    component: ChildComponent
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(routes)
  ]
})
export class ChildWrapperModule {

}

  • parent-wrapper.module.ts:
const routes: Routes = [
  {
    path: '',
    component: ParentComponent,
    children: [
      {
        path: '',
        loadChildren: () => import('./child-wrapper.module').then(m => m.ChildWrapperModule)
      }
    ]
  }
]

@NgModule({
  imports: [
    RouterModule.forChild(routes)
  ]
})
export class ParentWrapperModule {

}

  • app-routing.module.ts
const routes: Routes = [
  {
    path: '',
    loadChildren: () => import('./lazy-module/parent-wrapper.module').then(m => m.ParentWrapperModule)
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

This archieve my goal, but in my opinion, it seems to be STUPID, because when developers want to use ui-library, they have to re-declare the routing in the application.

Any better ideas please ?

1
  • I agree it seems wrong to have to redeclare the routes in the application that implements the parent library. Did you ever find a work around for this? Commented May 26, 2020 at 16:31

2 Answers 2

1
loadChildren: '/child/child.module#ChildModule'

Needs to be in the new Angular 8 import syntax

loadChildren: () => import('/child/child.module').then(m => m.ChildModule)

Not sure if you can lazy load modules built with the old pre Angular 8 syntax.

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

Comments

0

I also faced the same issue while implementing this functionality in my project. I implemented it in angular 6 though. I think the recommended solution given in the google comment is correct. In your main app you have to create a wrapper module for example libwrapper module which will only import the library module. And in app.module.ts in the routes declaration you can refer libwrapper module and this should fix your issue. Hipe this helps..

3 Comments

I have tried with wrapper module, but it seems not to work with nested module as above. Or am I doing wrong ?
From the posted scenario i will assume the childModule is part of the ui-lib library which as a whole is being lazy-loaded in the main app. Can you please prove the wrapper module that u have created in the main app?
The src has been updated in the question above. You can take a look :)

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.