Issue
I have a floating button in a custom component so I can reuse this component in several pages. When I inject this component into a page this works. But when I use this component in a modal then I get an error. This is my code for my float button.
Fabbutton.component.html
<div class="fixed">
<ion-fab (click)="scrollToTop()" vertical="bottom" horizontal="end" edge slot="fixed">
<ion-fab-button class="toolbar-color" size="small">
<ion-icon name="arrow-up"></ion-icon>
</ion-fab-button>
</ion-fab>
</div>
Fabbutton.component.css
.fixed {
position: fixed;
bottom: 85px;
right: 0;
}
Fabbutton.component.ts
import { Component, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'app-fabbutton',
templateUrl: './fabbutton.component.html',
styleUrls: ['./fabbutton.component.scss'],
})
export class FabbuttonComponent {
@Output('onClick') onClick = new EventEmitter<any>();
scrollToTop() {
this.onClick.emit();
}
}
In the modal page I use this component like this:
ListModal.page.html
<ion-header>
<ion-toolbar class="toolbar-color">
<ion-title>{{name}}</ion-title>
<ion-buttons slot="start">
<ion-button (click)="modalCtrl.dismiss()">
<ion-icon name="arrow-back-sharp" slot="icon-only"></ion-icon>
</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<app-fabbutton (onClick)="scrollToTop()"></app-fabbutton>
</ion-content>
ListModal.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { ListModalPageRoutingModule } from './listmodal-routing.module';
import { ListModalPage } from './listmodal.page';
import { FabbuttonComponent } from 'src/app/components/fabbutton/fabbutton.component';
@NgModule({
imports: [
CommonModule,
FormsModule,
IonicModule,
ListModalPageRoutingModule
],
declarations: [ListModalPage, FabbuttonComponent]
})
export class ListModalPageModule {}
I use this same proces for the pages and this works. But When I use this in a Modal I get the following error:
core.mjs:7626 ERROR Error: Uncaught (in promise): Error: Type FabbuttonComponent is part of the declarations of 2 modules: ListmodalPageModule and RecipesPageModule! Please consider moving FabbuttonComponent to a higher module that imports ListmodalPageModule and RecipesPageModule. You can also create a new NgModule that exports and includes FabbuttonComponent then import that NgModule in ListmodalPageModule and RecipesPageModule.
Error: Type FabbuttonComponent is part of the declarations of 2 modules: ListmodalPageModule and RecipesPageModule! Please consider moving FabbuttonComponent to a higher module that imports ListmodalPageModule and RecipesPageModule. You can also create a new NgModule that exports and includes FabbuttonComponent then import that NgModule in ListmodalPageModule and RecipesPageModule.
at verifySemanticsOfNgModuleDef (core.mjs:24393:15) at RecipesPageModule.get (core.mjs:24313:21) at getInjectorDef (core.mjs:482:13) at walkProviderTree (core.mjs:6473:18) at core.mjs:6433:13 at core.mjs:4193:76 at Array.forEach (<anonymous>) at deepForEach (core.mjs:4193:11) at core.mjs:4193:51 at Array.forEach (<anonymous>) at resolvePromise (zone.js:1262:35) at resolvePromise (zone.js:1216:21) at zone.js:1329:21 at _ZoneDelegate.invokeTask (zone.js:443:35) at Object.onInvokeTask (core.mjs:26339:33) at _ZoneDelegate.invokeTask (zone.js:442:64) at Zone.runTask (zone.js:214:51) at drainMicroTaskQueue (zone.js:632:39)
I have tried the following links to fix my error:
Why can I use my Fabbutton component in my pages but not in the modal page?
How can I use the Fabbutton component in my modal page?
Solution
Ok, if i understand correctly, the problem is you try to use a component in multiple modules (in ionic every page is a module). Angular not allow you to do that directly.
You have do create a module that export the components you have to use.
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { FabbuttonComponent } from 'src/app/components/fabbutton/fabbutton.component';
@NgModule({
imports: [ CommonModule ],
declarations: [ FabbuttonComponent ],
exports: [ FabbuttonComponent, CommonModule, FormsModule ]
})
export class FabbuttonSharedMobule { }
Now import that module where you need the component.
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';
import { ListModalPageRoutingModule } from './listmodal-routing.module';
import { ListModalPage } from './listmodal.page';
import { FabbuttonSharedModule } from 'src/app/common/fabbuttonshared.module';
@NgModule({
imports: [
CommonModule,
FormsModule,
FabbuttonSharedModule,
IonicModule,
ListModalPageRoutingModule
],
declarations: [ListModalPage]
})
export class ListModalPageModule {}
Answered By - Talon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.