Angular Component part of two declarations












1















I created a small component which I want to use in some of the pages but I seem to have some problems when including it in more than one lazy load page.



add.button.component.ts



import { Component, Output, EventEmitter } from '@angular/core';

@Component({
selector: 'add-button',
template: `
<ion-buttons end>
<button ion-button icon-only (click)="navToAdd()">
<ion-icon name="md-add-circle"></ion-icon>
</button>
</ion-buttons>`
})

export class AddButton {
visible: boolean = true;

@Output() navigate: EventEmitter<any> = new EventEmitter();

navToAdd() {
this.navigate.emit(null)
}
}


app.modules.ts



    import { BrowserModule } from '@angular/platform-browser';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';

@NgModule({
declarations: [
MyApp,
HomePage,
TabsPage
],
imports: [
BrowserModule,
...
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
TabsPage
],
providers: [
...
]
})
export class AppModule { }


users.module.ts - Just like users.module.ts there are other page modules where I want to use the component just like i use it in users.module.ts below



import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { UsersPage } from './users';
import { AddButton } from '../../components/add-button.component';

@NgModule({
declarations: [
UsersPage,
AddButton
],
imports: [
IonicPageModule.forChild(UsersPage),
],
})
export class UsersPageModule { }









share|improve this question

























  • I would create a Common-Module for those Components and include the Module in both other modules

    – enno.void
    Nov 21 '18 at 13:00
















1















I created a small component which I want to use in some of the pages but I seem to have some problems when including it in more than one lazy load page.



add.button.component.ts



import { Component, Output, EventEmitter } from '@angular/core';

@Component({
selector: 'add-button',
template: `
<ion-buttons end>
<button ion-button icon-only (click)="navToAdd()">
<ion-icon name="md-add-circle"></ion-icon>
</button>
</ion-buttons>`
})

export class AddButton {
visible: boolean = true;

@Output() navigate: EventEmitter<any> = new EventEmitter();

navToAdd() {
this.navigate.emit(null)
}
}


app.modules.ts



    import { BrowserModule } from '@angular/platform-browser';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';

@NgModule({
declarations: [
MyApp,
HomePage,
TabsPage
],
imports: [
BrowserModule,
...
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
TabsPage
],
providers: [
...
]
})
export class AppModule { }


users.module.ts - Just like users.module.ts there are other page modules where I want to use the component just like i use it in users.module.ts below



import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { UsersPage } from './users';
import { AddButton } from '../../components/add-button.component';

@NgModule({
declarations: [
UsersPage,
AddButton
],
imports: [
IonicPageModule.forChild(UsersPage),
],
})
export class UsersPageModule { }









share|improve this question

























  • I would create a Common-Module for those Components and include the Module in both other modules

    – enno.void
    Nov 21 '18 at 13:00














1












1








1








I created a small component which I want to use in some of the pages but I seem to have some problems when including it in more than one lazy load page.



add.button.component.ts



import { Component, Output, EventEmitter } from '@angular/core';

@Component({
selector: 'add-button',
template: `
<ion-buttons end>
<button ion-button icon-only (click)="navToAdd()">
<ion-icon name="md-add-circle"></ion-icon>
</button>
</ion-buttons>`
})

export class AddButton {
visible: boolean = true;

@Output() navigate: EventEmitter<any> = new EventEmitter();

navToAdd() {
this.navigate.emit(null)
}
}


app.modules.ts



    import { BrowserModule } from '@angular/platform-browser';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';

@NgModule({
declarations: [
MyApp,
HomePage,
TabsPage
],
imports: [
BrowserModule,
...
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
TabsPage
],
providers: [
...
]
})
export class AppModule { }


users.module.ts - Just like users.module.ts there are other page modules where I want to use the component just like i use it in users.module.ts below



import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { UsersPage } from './users';
import { AddButton } from '../../components/add-button.component';

@NgModule({
declarations: [
UsersPage,
AddButton
],
imports: [
IonicPageModule.forChild(UsersPage),
],
})
export class UsersPageModule { }









share|improve this question
















I created a small component which I want to use in some of the pages but I seem to have some problems when including it in more than one lazy load page.



add.button.component.ts



import { Component, Output, EventEmitter } from '@angular/core';

@Component({
selector: 'add-button',
template: `
<ion-buttons end>
<button ion-button icon-only (click)="navToAdd()">
<ion-icon name="md-add-circle"></ion-icon>
</button>
</ion-buttons>`
})

export class AddButton {
visible: boolean = true;

@Output() navigate: EventEmitter<any> = new EventEmitter();

navToAdd() {
this.navigate.emit(null)
}
}


app.modules.ts



    import { BrowserModule } from '@angular/platform-browser';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';

@NgModule({
declarations: [
MyApp,
HomePage,
TabsPage
],
imports: [
BrowserModule,
...
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage,
TabsPage
],
providers: [
...
]
})
export class AppModule { }


users.module.ts - Just like users.module.ts there are other page modules where I want to use the component just like i use it in users.module.ts below



import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { UsersPage } from './users';
import { AddButton } from '../../components/add-button.component';

@NgModule({
declarations: [
UsersPage,
AddButton
],
imports: [
IonicPageModule.forChild(UsersPage),
],
})
export class UsersPageModule { }






angular typescript ionic-framework ionic3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 15:15









Octavio Garbarino

472511




472511










asked Nov 21 '18 at 12:58









CiprianCiprian

74922558




74922558













  • I would create a Common-Module for those Components and include the Module in both other modules

    – enno.void
    Nov 21 '18 at 13:00



















  • I would create a Common-Module for those Components and include the Module in both other modules

    – enno.void
    Nov 21 '18 at 13:00

















I would create a Common-Module for those Components and include the Module in both other modules

– enno.void
Nov 21 '18 at 13:00





I would create a Common-Module for those Components and include the Module in both other modules

– enno.void
Nov 21 '18 at 13:00












4 Answers
4






active

oldest

votes


















2














Your AddButton should probably be on a Shared module and then you would be able to use it in multiple places by importing the module






share|improve this answer
























  • Ok ... Seems simple enough to create a shared module. I don't know where to include it. Do I include the shared module in all page modules? Like users.module.ts, events.module.ts? What should users.module.ts look like?

    – Ciprian
    Nov 21 '18 at 13:23





















1














add your AddButton component into exports array of users.module.ts so you can use it in other modules where UsersModule is imported



import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { UsersPage } from './users';
import { AddButton } from '../../components/add-button.component';

@NgModule({
declarations: [
UsersPage,
AddButton
],
imports: [
IonicPageModule.forChild(UsersPage),
],
exports: [
AddButton
]
})
export class UsersPageModule { }





share|improve this answer































    0














    If you create the component with the ionic cli with ionic g component AddButton it will automatically create a ComponentsModule and your new component in that module.



    And to use this component in the other modules you just have to import that module in the modules that you need.



    Also in your ComponentModule you should add imports: [IonicModule] in order to be able to use the Ionic Components like ion-buttons






    share|improve this answer































      0














      If you think ButtonComponent doesn't fit to be in Shared Module, Make a Individual module for AddButton. Now this new module should declare and export the AddButtonComponent. You should import this new module in whichever child module you want it to be used for. This should not cause any problem. This will also save you from loading all Shared component just for sake of getting AddComponent working.




      It's a best practice to create another module for the component if it is being used at multiple places/modules.







      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412562%2fangular-component-part-of-two-declarations%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        Your AddButton should probably be on a Shared module and then you would be able to use it in multiple places by importing the module






        share|improve this answer
























        • Ok ... Seems simple enough to create a shared module. I don't know where to include it. Do I include the shared module in all page modules? Like users.module.ts, events.module.ts? What should users.module.ts look like?

          – Ciprian
          Nov 21 '18 at 13:23


















        2














        Your AddButton should probably be on a Shared module and then you would be able to use it in multiple places by importing the module






        share|improve this answer
























        • Ok ... Seems simple enough to create a shared module. I don't know where to include it. Do I include the shared module in all page modules? Like users.module.ts, events.module.ts? What should users.module.ts look like?

          – Ciprian
          Nov 21 '18 at 13:23
















        2












        2








        2







        Your AddButton should probably be on a Shared module and then you would be able to use it in multiple places by importing the module






        share|improve this answer













        Your AddButton should probably be on a Shared module and then you would be able to use it in multiple places by importing the module







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 13:03









        BrunoLMBrunoLM

        60.8k67228389




        60.8k67228389













        • Ok ... Seems simple enough to create a shared module. I don't know where to include it. Do I include the shared module in all page modules? Like users.module.ts, events.module.ts? What should users.module.ts look like?

          – Ciprian
          Nov 21 '18 at 13:23





















        • Ok ... Seems simple enough to create a shared module. I don't know where to include it. Do I include the shared module in all page modules? Like users.module.ts, events.module.ts? What should users.module.ts look like?

          – Ciprian
          Nov 21 '18 at 13:23



















        Ok ... Seems simple enough to create a shared module. I don't know where to include it. Do I include the shared module in all page modules? Like users.module.ts, events.module.ts? What should users.module.ts look like?

        – Ciprian
        Nov 21 '18 at 13:23







        Ok ... Seems simple enough to create a shared module. I don't know where to include it. Do I include the shared module in all page modules? Like users.module.ts, events.module.ts? What should users.module.ts look like?

        – Ciprian
        Nov 21 '18 at 13:23















        1














        add your AddButton component into exports array of users.module.ts so you can use it in other modules where UsersModule is imported



        import { NgModule } from '@angular/core';
        import { IonicPageModule } from 'ionic-angular';
        import { UsersPage } from './users';
        import { AddButton } from '../../components/add-button.component';

        @NgModule({
        declarations: [
        UsersPage,
        AddButton
        ],
        imports: [
        IonicPageModule.forChild(UsersPage),
        ],
        exports: [
        AddButton
        ]
        })
        export class UsersPageModule { }





        share|improve this answer




























          1














          add your AddButton component into exports array of users.module.ts so you can use it in other modules where UsersModule is imported



          import { NgModule } from '@angular/core';
          import { IonicPageModule } from 'ionic-angular';
          import { UsersPage } from './users';
          import { AddButton } from '../../components/add-button.component';

          @NgModule({
          declarations: [
          UsersPage,
          AddButton
          ],
          imports: [
          IonicPageModule.forChild(UsersPage),
          ],
          exports: [
          AddButton
          ]
          })
          export class UsersPageModule { }





          share|improve this answer


























            1












            1








            1







            add your AddButton component into exports array of users.module.ts so you can use it in other modules where UsersModule is imported



            import { NgModule } from '@angular/core';
            import { IonicPageModule } from 'ionic-angular';
            import { UsersPage } from './users';
            import { AddButton } from '../../components/add-button.component';

            @NgModule({
            declarations: [
            UsersPage,
            AddButton
            ],
            imports: [
            IonicPageModule.forChild(UsersPage),
            ],
            exports: [
            AddButton
            ]
            })
            export class UsersPageModule { }





            share|improve this answer













            add your AddButton component into exports array of users.module.ts so you can use it in other modules where UsersModule is imported



            import { NgModule } from '@angular/core';
            import { IonicPageModule } from 'ionic-angular';
            import { UsersPage } from './users';
            import { AddButton } from '../../components/add-button.component';

            @NgModule({
            declarations: [
            UsersPage,
            AddButton
            ],
            imports: [
            IonicPageModule.forChild(UsersPage),
            ],
            exports: [
            AddButton
            ]
            })
            export class UsersPageModule { }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 13:03









            Artyom AmiryanArtyom Amiryan

            1,9621215




            1,9621215























                0














                If you create the component with the ionic cli with ionic g component AddButton it will automatically create a ComponentsModule and your new component in that module.



                And to use this component in the other modules you just have to import that module in the modules that you need.



                Also in your ComponentModule you should add imports: [IonicModule] in order to be able to use the Ionic Components like ion-buttons






                share|improve this answer




























                  0














                  If you create the component with the ionic cli with ionic g component AddButton it will automatically create a ComponentsModule and your new component in that module.



                  And to use this component in the other modules you just have to import that module in the modules that you need.



                  Also in your ComponentModule you should add imports: [IonicModule] in order to be able to use the Ionic Components like ion-buttons






                  share|improve this answer


























                    0












                    0








                    0







                    If you create the component with the ionic cli with ionic g component AddButton it will automatically create a ComponentsModule and your new component in that module.



                    And to use this component in the other modules you just have to import that module in the modules that you need.



                    Also in your ComponentModule you should add imports: [IonicModule] in order to be able to use the Ionic Components like ion-buttons






                    share|improve this answer













                    If you create the component with the ionic cli with ionic g component AddButton it will automatically create a ComponentsModule and your new component in that module.



                    And to use this component in the other modules you just have to import that module in the modules that you need.



                    Also in your ComponentModule you should add imports: [IonicModule] in order to be able to use the Ionic Components like ion-buttons







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 13:07









                    Octavio GarbarinoOctavio Garbarino

                    472511




                    472511























                        0














                        If you think ButtonComponent doesn't fit to be in Shared Module, Make a Individual module for AddButton. Now this new module should declare and export the AddButtonComponent. You should import this new module in whichever child module you want it to be used for. This should not cause any problem. This will also save you from loading all Shared component just for sake of getting AddComponent working.




                        It's a best practice to create another module for the component if it is being used at multiple places/modules.







                        share|improve this answer




























                          0














                          If you think ButtonComponent doesn't fit to be in Shared Module, Make a Individual module for AddButton. Now this new module should declare and export the AddButtonComponent. You should import this new module in whichever child module you want it to be used for. This should not cause any problem. This will also save you from loading all Shared component just for sake of getting AddComponent working.




                          It's a best practice to create another module for the component if it is being used at multiple places/modules.







                          share|improve this answer


























                            0












                            0








                            0







                            If you think ButtonComponent doesn't fit to be in Shared Module, Make a Individual module for AddButton. Now this new module should declare and export the AddButtonComponent. You should import this new module in whichever child module you want it to be used for. This should not cause any problem. This will also save you from loading all Shared component just for sake of getting AddComponent working.




                            It's a best practice to create another module for the component if it is being used at multiple places/modules.







                            share|improve this answer













                            If you think ButtonComponent doesn't fit to be in Shared Module, Make a Individual module for AddButton. Now this new module should declare and export the AddButtonComponent. You should import this new module in whichever child module you want it to be used for. This should not cause any problem. This will also save you from loading all Shared component just for sake of getting AddComponent working.




                            It's a best practice to create another module for the component if it is being used at multiple places/modules.








                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 13:33









                            nircraftnircraft

                            1,702521




                            1,702521






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412562%2fangular-component-part-of-two-declarations%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()