Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4 (Angular 7) - sharing components issue

Tags:

angular

ionic4

I'm trying to do an extremely usual thing for such a framework like Angular. The goal is to use the same (HeaderComponent) component more than once through a shared module.

My shared.module.ts:

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { HeaderComponent } from '../header/header.component';
import { IonicModule} from '@ionic/angular';

@NgModule({
    imports: [
        CommonModule, 
        IonicModule
    ],
    declarations: [
        HeaderComponent
    ],
    exports: [
        HeaderComponent
    ]
})

export class SharedModule {}

In app.module.ts I added this:

imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, SharedModule],

And in home.page.html I tried to render it this way:

<app-header></app-header>

It actually worked since browser showed me errors like

'ion-col' is not a known element

and so on for all the ionic components from the HeaderComponent.

I've found a solution for the issue over the Internet that suggest adding IonicModule.forRoot(HeaderComponent) to the imports array of the shared.module.ts, but this approach causes the following error:

'app-header' is not a known element

As if it is no longer available.

like image 314
Supervision Avatar asked Oct 27 '18 09:10

Supervision


People also ask

What is @ionic/angular?

@ionic/angular combines the core Ionic experience with the tooling and APIs that are tailored to Angular Developers. Ionic supports Angular 6.0.0 and up.

What is Ionic 4’s angular routing?

One of Ionic 4’s key features is its capability to use Angular routing. The Angular router implements a path/component lookup. In other words, Angular routing works like URLs on a web browser.

Is it better to use ionic core or angular?

It provides all Ionic components as Angular components. Angular components can expose its type information which is recognized by Angular template compilers. If you want to use many Ionic components in Angular, or use in complex, it is better to use Ionic Angular instead of Ionic Core.

How does angular router work with Ionic?

The Angular router implements a path/component lookup. In other words, Angular routing works like URLs on a web browser. You can reference an Ionic page by calling /your-page-name. When an app loads, the Angular router begins by reading the URL being loaded.


Video Answer


1 Answers

you additionally have to add the ionic module to your shared module like this:

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { HeaderComponent } from '../header/header.component';
import { IonicModule } from 'ionic-angular';

@NgModule({
  imports: [
    CommonModule,
    IonicModule
  ],
  declarations: [
    HeaderComponent
  ],
  exports: [
    HeaderComponent
  ]
})

export class SharedModule {}

if you are using ionic 4 you have to edit the import of IonicModule to this:

import { IonicModule } from '@ionic/angular';
like image 80
Gary Großgarten Avatar answered Oct 20 '22 04:10

Gary Großgarten