I’ve created a component named ContextMenuComponent that will open a popover when a button is clicked. I created another component for the popover, named ContextMenuPopoverComponent. Both files are in the same folder.
I have a module where I list all the components for my app: src/components/components.module.ts. I have declared both components in there:
I was testing some things and I was able to make it work by importing the CoreComponentsModule in my AppModule (src/app/app.module.ts):
import { CoreComponentsModule } from '../components/components.module';
...
imports: [
CoreComponentsModule
I tried removing the import of CoreComponentsModule from the page’s module because now I’m importing it in the app’s module, but if I remove it then my app stops working.
Is it ok to import the components module both in the app and in the page? Should I do it different?
in order for this to work I think I should declare the popover as a IonicPage and create a module to lazy load it. I was trying to make it work without the lazy load
Don’t declare any components as entryComponents. Declaring them imperatively like that defeats lazy loading, because you need to eagerly provide a factory for them somewhere.
Even though it’s an old post, this may be helpful for any person encountering the same problem, as I just did.
I found an explanation here why in this case you need to import the component’s module in the app module. Basically, if you need to navigate to the component, e.g. as part of a popover or modal, you need to import the component in the app module and also declare it as entryComponents either in the app module or component module.
Note: It shouldn’t be necessary to import the component’s module also in the page’s module as you say, it should be enough to import it in the app module.
Also, AaronSterling’s comment that you shouldn’t declare the component as entryComponents doesn’t seem correct in this case. It didn’t work for me without declaring it as entryComponent.
By the way, as explained here under ‘Important Note’, you may not even need to create a module for your component, which also means that you could get rid of importing your component’s module in the app module. You would just include the component in the declarations and entryComponents sections of the module of the page that uses the component.
This works for me in a new Ionic 4 project I’ve created, but not in an Ionic 3 project. I guess it depends on the Ionic / Angular version you use.
Following should be the structure of the page after popover,named as MoreOptionsComponent,creation:@NgModule({
imports: [
IonicModule,
CommonModule,
FormsModule,
RouterModule.forChild([{ path: ‘’, component: Tab2Page },{path: ‘’, component: MoreOptionsComponent}])
],
declarations: [Tab2Page, ScrollableDirective, LoadingSpinnerComponent,MoreOptionsComponent],
entryComponents:[Tab2Page,]
})strong text