0

i have a project that is lazy loaded, it has 2 modules view.module.ts and view2.modules.ts other than module.ts. i want to use a component called editform.component.ts in both modules. is it possible??

Asifali
  • 207
  • 3
  • 10
  • Yes. It should be declared and exported in a shared module, and the two lazy-loaded modules should import this shared module. – JB Nizet Nov 22 '19 at 11:21
  • Are you looking for this? [Angular - Using a component across multiple modules](https://stackoverflow.com/questions/47305169/angular-using-a-component-across-multiple-modules) – hbamithkumara Nov 22 '19 at 11:28
  • its pretty simple –  Nov 22 '19 at 11:36

2 Answers2

5

To use component you can use a shared module.

@NgModule({
    imports: [
        SomeModule
     ],
    declarations: [
         SharedComponent
    ],
    exports: [
        SharedComponent
    ]
})

export class SharedModule {}

Now inside that shared module you can add the component that you want to share. Do not forget to put them into exports.

Now add this module into your app.module.ts

import { SharedModule } from './shared/shared.module;

@NgModule({
    imports: [ SharedModule],
     ...
})

Now you can use the shared component via app.module.ts. Hope this might solve your problem.

Kazi
  • 1,461
  • 3
  • 19
  • 47
1

You can't use a component that imported in a different module. You should have shared component with his sharedModule, then import that module to your module. And also add that component in your sharedModule's "exports" section.