Importing custom Ionic 3 components

I'm stuck importing a custom component to a page in Ionic 3. This was relatively trivial in Ionic 2, but I can't get it to work in Ionic 3.

I have an existing page module named other

. Once launched, ionic g component test

I import the auto-generated ComponentsModule

in other.module.ts

and add it to the array imports

.

import { ComponentsModule } from "../../components/components.module";
@NgModule({
  declarations: [
    OtherPage,
  ],
  imports: [
    IonicPageModule.forChild(OtherPage),
    ComponentsModule,

    ],
})
export class OtherPageModule {}

      

Then I add a component selector to the page as <test></test>

.

This will result in an error:

polyfills.js:3 Unhandled Promise rejection: Template parse errors:
'test' is not a known element:
1. If 'test' is an Angular component, then verify that it is part of this module.
2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("

<ion-content padding>
[ERROR ->]<test> </test>
</ion-content>
"): ng:///AppModule/OtherPage.html@16:0 ; Zone: <root> ; Task: Promise.then ; Value: Error: Template parse errors:
'test' is not a known element:
1. If 'test' is an Angular component, then verify that it is part of this module.

      

It doesn't work for me. I also tried building module

for a test component and importing it in the same way, but that doesn't work for me.

I cannot find any documentation or examples for this. How is the custom component imported in Ionic 3?

+7


source to share


8 answers


This is how I solved it:

  • Let's say your custom-component.ts is exporting a class CustomComponent

    .
  • import the same in custom-component.module.ts as shown below:

    import { CustomComponent } from './custom-component';
    
    @NgModule({
      declarations: [
        CustomComponent,
      ],
      imports: [
        IonicPageModule.forChild(CustomComponent),
      ],
      exports : [ CustomComponent ]
    })
    export class CustomComponentPageModule { }
    
          

  • Now import your custom component to the page you want as shown below:

    import { ContentDrawerPageModule } from '../../../components/custom-component/custom-component.module';
    
    @NgModule({
      declarations: [
        HelloWorldPage,
      ],
      imports: [
        IonicPageModule.forChild(HelloWorldPage),
        CustomComponentPageModule,
      ]
    })
    export class HelloWorldPageModule {}
    
          



And you have successfully imported your custom component ( CustomComponent

) to your page (HelloWorldPage).

+3


source


As stated here https://github.com/ionic-team/ionic/issues/11560

Unfortunately there is no way to make a lazy load component, directive, pipe. in any way let us know if you can handle it.



So, you can import your ComponModule into your page.module.ts. Here's mine.

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ComponentsModule } from ../../components/components.module';

@NgModule({
  declarations: [
    WoDetailsPage,

  ],
  imports: [
    ComponentsModule,
    IonicPageModule.forChild(WoDetailsPage),
  ],
})
export class WoDetailsPageModule {}

      

+1


source


you need to do it like below:

import { ComponentsModule } from "../../components/components.module";
@NgModule({
  declarations: [
    OtherPage,
    ComponentsModule,
  ],
  imports: [
    IonicPageModule.forChild(OtherPage),

    ],
  entryComponents: [
    ComponentsModule
  ]
})
export class OtherPageModule {}

      

0


source


You must export the component test

.

@NgModule({
  declarations: [
    TestComponent,
  ],
  imports: [],
  exports: [TestComponent]
})
export class ComponentModule {}

      

0


source


One way is to add each component to your app.module.ts file and then declare the following:

import { MyComp } from "../../components/my-comp/my-comp";
@NgModule({
  declarations: [
    OtherPage,
    MyComp
  ],
  imports: [
    IonicPageModule.forChild(OtherPage)
    ],
})
export class OtherPageModule {}

      

0


source


After successfully using CUSTOM_ELEMENTS_SCHEMA declarations in my modules, which forces you to call your selectors with a "-" trait, so I'll have to use something like:

<MyPlayer-comp></MyPlayer-comp>

      

I was saddened by this complete nonsensical behavior of these frameworks on camel suitcases, even if it worked and ngc no longer complained about "... unknown element ...".

I personally prefer to call everything the same (class, selector, file, etc ...)

I went back to the next one, which works with lazy loading while I'm building for the web. (Ionic Cordova for browser build --prod - release)

Your custom component module declarations:

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
//
import { MyPlayerComp } from './MyPlayerComp ';
//
@NgModule({
    declarations: [MyPlayerComp ],
    imports: [IonicPageModule.forChild(MyPlayerComp )],
    exports: [MyPlayerComp ],
})
export class MyPlayerCompModule { }

      

In modules where you want to use your custom component, use the following:

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
//
import { PlyrView } from './PlyrView';
//
import { MyPlayerCompModule } from './MyPlayerComp.module' // 20180720
//
@NgModule({
    declarations: [PlyrView],
    imports: [
        IonicPageModule.forChild(PlyrView),
        MyPlayerCompModule,                      // <<<<<
    ],                
    exports: [PlyrView],
})
export class PlyrViewModule { }

      

I hope this helps someone

0


source


This is how I solved it by following the link below:
http://evalogical.com/blog/components-ionic-framework-3

  1. Import the component to the file home.ts

    as follows:
    import { MyComponent } from '../../components/my/my';

  2. In home.module.ts

    import ComponentsModule

    and add it to import, like this.

    import { NgModule } from '@angular/core';  
    import { IonicPageModule } from 'ionic-angular';  
    import { HomePage } from './home';  
    import { ComponentsModule } from '../../components/components.module';  
    @NgModule({  
    declarations: [  
    HomePage,  
    ],  
    imports: [  
    IonicPageModule.forChild(HomePage),  
    ComponentsModule  
    ],  
    })  
    export class HomePageModule {}
    
          

  3. Import the IonicPageModule to a file components.module.ts

    , and here we need to import CUSTOM_ELEMENTS_SCHEMA

    in schemas like this.

    import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';  
    import { MyComponent } from './my/my';  
    import { IonicPageModule } from 'ionic-angular';  
    @NgModule({  
    declarations: [MyComponent],  
    imports: [IonicPageModule.forChild(MyComponent)],  
    exports: [MyComponent],  
    schemas: [CUSTOM_ELEMENTS_SCHEMA]  
    })  
    export class ComponentsModule {}  
    
          

  4. Remove line
    import { HomePage } from '../pages/home/home';

    from file app.module.ts

    and add the following line insteadimport { HomePageModule } from '../pages/home/home.module';

  5. Now you can use component selector as HTML tag in file home.html

0


source


If myComponent.ts is your component.

Then you can remove the generated module with ionic generate commands (if you are not using lazy loading) and add the following directly to app.module.ts

    @NgModule({
      declarations: [
        MyComponent,
      ]
      entryComponents: [
        MyComponent
      ] });

      

And myComponent.ts looks like this:

@Component({
  selector: 'app-sheduler',
  templateUrl: 'sheduler.html'
})
export class ShedulerComponent {}

      

0


source







All Articles