Ionic 4 - How to put a component inside another component/page (nested)

Hi:

I’m migrating from 2 to 4 beta.

I want to put a component inside another component and it gives me hard time.

in ionic 2 I could easily put in the html of a page/component the selector of a component I created like so:

and it would work like a charm.

Now it gives me this error:

//----------------------------------------------
ERROR Error: Uncaught (in promise): Error: Template parse errors:
‘my-custom-component’ is not a known element:

  1. If ‘my-custom-component’ is an Angular component, then verify that it is part of this module.
  2. If ‘my-custom-component’ is a Web Component then add ‘CUSTOM_ELEMENTS_SCHEMA’ to the ‘@NgModule.schemas’ of this component to suppress this message.
    //----------------------------------------------

What should I do?

10X in advance:)

hello,
your components from ionic 2 are angular components.
I assume you have created in ionic 4 an angular components too.
If you use visual code, then there are extensions that help with importing.
Import to that module you want integrate the other component the module of your componet like

import {whateverComponentModule} from '.../whatever/whatever.component.module;

add whateverComponentModule to the import section of ngmodule.

Maybe it is nessecarry to restart visual code or similar.

Take a look into your whatever.ts, what your selector is and use it as html tag.

Best regards, anna-liebt

Hן:

Thanks for answering.

About your answer, there is no whatever.component.module file generated in ionic 4 beta.

I added the compomnent like so: (copy paste selector)

Doesn’t work.

What now?

Hello,
maybe you should use ionic generate for creating pages, componets, pipes, etc. This do the most work for you. Copy paste then missing code into the generated files.

You can also add your component to app.module to make it eager loaded.

Best regards, anna-liebt

In your component page module add the component to declarations array:

import { TemplateComponent } from "./templates/component.temp";

@NgModule({
....
declarations: [
TemplateComponent
]
....
});

and component.temp.ts could be like:

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

@Component({
    selector: 'template-component',
    templateUrl: 'component.temp.html'
})

export class TemplateComponent {
    constructor() {
        
    }
}

then, add to your main page component

note: I put .ts and .html templates files on same directory