Component not working

I’m having trouble getting a basic component to work. Have followed a nunber of tuorials with no luck.

Started by generating a component with "ionic g component testcomp"
Added the import to my app.module.ts, and the module name “Testcomp” to the declaration.
Added the component to my home page html

get error:
Error: Uncaught (in promise): Error: Template parse errors:
‘testcomp’ is not a known element:

I notice that the generator gives me a module file with IonicPageModule in it, no tutorial mentions that.

Any ideas?

versions:
Ionic Framework: 3.1.1
Ionic App Scripts: 1.3.7
Angular Core: 4.0.2
Angular Compiler CLI: 4.0.2
Node: 7.7.3
OS Platform: Windows 10
Navigator Platform: Win32
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36

Did you use a right selector? You can use that module file for fast import(just import module in app.module.ts) or for lazy-loading.

Yes I checked the selector a few times, tried changing to include a hyphen incase that was important too.
How is the module used for lazy loading then?
Here’s my relavant code sections:-

“testcomp.ts”

import { Component } from '@angular/core';

@Component({
  selector: 'test-comp',
  templateUrl: 'testcomp.html'
})
export class Testcomp {
  text: string;
  constructor() {
    this.text = 'Hello World';
  }
}

“testcomp.html”
{{text}}

and in app.module.ts

...
import { Testcomp } from '../components/testcomp/testcomp';
...
@NgModule({
  declarations: [MyApp, Testcomp],
  imports: [
  .....

and referenced in my home page html here:

<test-comp></test-comp>

You have to add your component to import:

@NgModule({
  declarations: [MyApp, Testcomp],
  imports: [
    BrowserModule,
    HttpModule,
    HttpInterceptorModule,
    IonicModule.forRoot(MyApp),
    Testcomp
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
  ]
})

something like that

1 Like

That gives me this error:

Runtime Error
Unexpected directive 'Testcomp' imported by the module 'AppModule'. Please add a @NgModule annotation

Do I require the testcomp.module.ts files that was generated?.I tried it in and out, it contains this:

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { Testcomp } from './testcomp';

@NgModule({
  declarations: [
    Testcomp,
  ],
  imports: [
    IonicPageModule.forChild(Testcomp),
  ],
  exports: [
    Testcomp
  ]
})
export class TestcompModule {}

@petebreeves add Testcomp to entryComponents in your app.module.ts

Well then, try this:

@NgModule({
  declarations: [MyApp],
  imports: [
    BrowserModule,
    HttpModule,
    HttpInterceptorModule,
    IonicModule.forRoot(MyApp),
    TestcompModule
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
  ]
})

Same error, unknown component.
Also tried adding to entryComponents, on its own and plus combinations of the other things.

I’ve watched a tutorial on this, it looked so simple.

It shoud be simple. Check build files, is you component there?

@petebreeves can you post app.module.ts I just noticed that you’re using Ionic v3

Yes, ionic 3.1.1
I’ve made no changes to app.component.ts, should I need to?

no I mean app.module.ts

found this in main.js, so yes it looks like it’s being picked up:


var Testcomp = (function () {
    function Testcomp() {
        this.text = 'Hello World';
    }
    return Testcomp;
}());
Testcomp = __decorate([
    __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__angular_core__["_5" /* Component */])({
        selector: 'test-comp',template:

app.module.ts in whole form is this:

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { IonicStorageModule } from '@ionic/storage';
import { MyApp } from './app.component';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { FormData } from "../providers/form-data";
import { RecipientData } from "../providers/recipient-data";
import { AuthService } from './../providers/auth-service';
import { SettingsService } from './../providers/settings-service';
import { SpidService } from './../providers/spid-service';

import { LoginHeadComponent } from '../components/login-head/login-head';
import { Testcomp } from '../components/testcomp/testcomp';

@NgModule({
  declarations: [MyApp, LoginHeadComponent, Testcomp],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    HttpModule,
    IonicStorageModule.forRoot()
    
   ],
  bootstrap: [IonicApp],
  entryComponents: [MyApp],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    FormData,
    RecipientData,
    AuthService,
    SettingsService,
    SpidService
  ]
})
export class AppModule {}

I’ve tried the same thing on a new app and it works. I’ll compare for tiny differences, but does is make a difference that all my pages are being lazy-loaded?

So I took that working component and dropped it in my exisiting app, back to “unknown component”.

…and returning the Homepage back into non lazy-load style makes it work!

So new question - how does a custom component get used in a lazy-loaded page?

The custom component must have its own module that is imported by the lazy page’s. Syntax is in this post. Beware that your app binary will end up having extra copies of your component code, one for each page that imports it.

thanks for you help on this subject so far.
I think I’ll go back to having declared pages from the start, I just didn’t like having page references everywhere, feels like a backwards step.

Did you import the CUSTOM_ELEMENTS_SCHEMA to your .module file , then add it to the schemas[] ?

import { ErrorHandler, NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';

then

schemas: [CUSTOM_ELEMENTS_SCHEMA ]