S
S
sdgroup142019-07-04 10:54:13
Angular
sdgroup14, 2019-07-04 10:54:13

Is there another solution for shearedModule?

Hello. I am writing an application, and I encountered such an unpleasant feature. There are many lazy modules (pages) in which I import the same component, respectively, I get an error that I need to make a sharedModule. But it turns out that, for example, there are 5 pages, and there is a sharedModule, and 2 components are shared between 3 pages, and 1 component is shared between 2 pages, and 1 more component is shared between 5 modules. I mean that I had to create a bunch of shared modules to avoid mistakes, it turns out I have 1 shared module for 1 component. Generally something like this
3 lazy pages

@NgModule({
  declarations: [],
  imports: [
    Shared1Module
  ]
})

export class Lazy1Module {
}

@NgModule({
  imports: [
     Shared2Module
  ]
})

export class Lazy2Module {
}

@NgModule({
  imports: [
    Shared2Module
  ],
})

export class Lazy3Module {
}

3 components
export class My1Component {
}

export class My2Component {
}

export class My3Component {
}

and 2 lazy
@NgModule({
  declarations: [
     My1Component,
     My2Component
  ],
 exports: [My1Component, My2Component]
})

export class Shared1Module {
}

@NgModule({
  declarations: [
     My3Component
  ],
 exports: [My3Component]
})

export class Shared2Module {
}

I don't understand, is there really no other solution? If so, why do you need to do this at all? It's just that the project has grown, up to 100+ individual components in which there are still small components, and it turns out, I either make a separate shared for a separate component, or I need to look for a connection between the entire project and this component and make some kind of shared. In general, already 30 shared and this is very inconvenient. I hope you got what I meant :). Please advise on this issue :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-07-04
@sdgroup14

1 component - 1 module, quite a normal solution. Of course they should have the same name, not "Shared2" and stupid components are also in this module.
Frequently used ones can already be collected in Shared, as a rule, it is one per project or section. If it loads several unused modules in the current lazy module, it's okay.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question