Having trouble getting the generated component from Ionic v4 CLI to function properly

I'm currently facing a challenge while working with the latest version of Ionic. I am struggling to make a CLI-generated component work properly.

After starting with a blank project, I create a new component using the following command:

ionic generate component my-component

The command runs successfully and generates the following files:

CREATE src/app/my-component/my-component.component.html (31 bytes)
CREATE src/app/my-component/my-component.component.spec.ts (664 bytes)
CREATE src/app/my-component/my-component.component.ts (293 bytes)
CREATE src/app/my-component/my-component.component.scss (0 bytes)

Next, I attempt to use the newly created component in my main page as follows:

<ion-content padding>
<my-component></my-component>
</ion-content>

I update the app.module.ts file accordingly:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouterModule, RouteReuseStrategy, Routes } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { MyComponentComponent } from './my-component/my-component.component';

@NgModule({
  declarations: [AppComponent, MyComponentComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

However, upon running the app in ionic lab, I encounter the following error:

ERROR Error: Uncaught (in promise): Error: Template parse errors: 'my-component' is not a known element

Here are the details of my system:

ionic (Ionic CLI)             : 4.2.1 
Ionic Framework               : @ionic/angular 4.0.0-beta.12
@angular-devkit/build-angular : 0.7.5
@angular-devkit/schematics    : 0.7.5
@angular/cli                  : 6.1.5
@ionic/angular-toolkit        : 1.0.0

Any insights on why this issue is occurring? I have previously worked with Ionic 3 without encountering this problem.

update:

Below is the default content of my-component.component.ts file:

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

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss']
})
export class MyComponentComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

Answer №1

To incorporate a custom component within another component, you must include it in the exports array.

@NgModule({
  ....
  declarations: [AppComponent, MyComponentComponent],
  entryComponents: [],
  exports:[MyComponentComponent]
  ....
})
export class AppModule {}

You have two options: you can either follow this method or create all your custom components within a separate module called customModule, and then import that module into the app.component.ts page.

The selector name for your component is incorrect. The selector for your MyComponentComponent class is actually app-my-component, so you should use

<app-my-component></app-my-component>
instead of
<my-component></my-component>
.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could have occurred if you reassigned setInterval to a variable within the useEffect hook?

Can multiple setInterval functions be defined repeatedly to the same variable in a React hook useEffect? After checking, I found that the variable has a new setInterval id value every time it is defined. However, I am curious if there are any instances re ...

Unauthorized Access with Ajax jQuery

While working on an ajax request using Jquery, I encountered a dilemma. Even though the page is only accessible to logged in users, my cookies are not being sent along with the request. This results in a 401 error. I am already logged in, so why aren' ...

Implementing an Angular HttpInterceptor to improve caching efficiency for simultaneous requests by utilizing a shared observable

I am looking to implement caching for HTTP parallel requests by sharing the observable and also storing the response in a Map object. Check out the online demo caching-interceptor.service.ts import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest ...

Instructions for adding a Key every time a user clicks the space Key within an Input field

I built a Movie Search Page with the OMDB API. My current issue is that the API returns an error when searching for a movie with more than one word because the API URL requires a + key between each word. I am looking for a way to automatically add the + ke ...

I'm having trouble understanding why my data does not appear even though there is space allocated for it automatically

I'm facing an issue with my code that is supposed to populate a table with dynamic content using react and ant.design. Everything seems to be working fine, but the actual content is not displaying - only the space for it is created. What's happe ...

Creating SQL statements in PHP programming

I am struggling to take all the values from the post array and incorporate each one into a select statement that updates the preceding value. My issue lies in executing this process in PHP due to difficulties with string escaping. This represents the SQL ...

Creating a form for adding and editing using React Hook Form

I'm currently working on creating a form that can handle both the creation and editing of a product by passing values through an object. The form functions perfectly for creating a product, but I'm facing challenges in making it work for editing ...

The Splice function is malfunctioning due to issues with the object (the indexOf function is not recognized)

I am currently dealing with an object that looks like this: Object {val1: "Hello", val2: "", dt1: "pilo1", dt2: "pilo2", lo1: "log1"} My goal is to remove any keys within the object that have empty values (""). I attempted the following code snippet: ...

error fetching data: unable to access due to client blocking

Despite including all possible information in my request, I am still encountering the same error. How can I identify the root cause of this issue? I have already disabled my AdBlocker extension. await fetch('http://127.0.0.1:8080/hxo.json?dummy=2s21&a ...

What is preventing me from implementing my JavaScript event on my EJS code?

Looking to add a click event to the image in my EJS file, here's the EJS code snippet: <div class="container" id="comments"> <div class="row"> <div class="col-lg-12"> <div class="well"> ...

IE does not support hover effects

Having trouble with a hover effect that works in Chrome but not in MSIE. Are there any alternatives or fixes to make it work in MSIE? The hover/rollover effects I've tried all seem to have this issue in IE. normalize.css demo.css set2.css font- ...

How can the bootstrap mega menu be modified to use an onclick function and incorporate drop-down items for mobile users?

Is it possible to change the drop-down menu behavior for small devices to make it work on click instead of hover? The issue I'm facing is that the sub-menus are appearing outside the root navigation on mobile devices. How can I modify the code to ensu ...

Tips for implementing Conditional validation in form models in anugular2

I need to determine whether the required validator for Address should be applied based on the value of this.type being 2. Below is the code snippet I am using for form validation: buildForm() { this.orgForm = this.fb.group({ Name: [this.addUpd ...

The schema for model "User, userSchema" has not been registered, resulting in a MissingSchemaError. To fix this error, make sure to use mongoose.model(name, schema

Hey there, I hope everything is going well! I'm currently working through a tutorial from 2019 and running into an error. Here's the code snippet (let me know if you need more information): // index.js // const express = require('express&a ...

I'm encountering a problem with handling errors in Express.js: A critical error has occurred while attempting to execute the _runMicro

Currently, I am utilizing the Blizzard API Battle.Net to fetch information regarding a character's name and the realm they inhabit. In certain cases, a user may request a character that does not exist, prompting Blizzard to return a 404 error response ...

Eliminate set of dates utilizing a different set of dates

Utilizing the MultipleDatePicker plugin to enable selection of multiple dates within a year. I have incorporated a checkbox feature that, when checked, will automatically mark all Sundays in the calendar. However, an issue arises when unchecking the check ...

Utilize the Stripe Payment Gateway within your Cordova/Phonegap Application

I have spent a considerable amount of time searching for a solution. I am looking to incorporate Stripe Payment Gateway into my cordova application. Is there a method to achieve this on both Android and iOS using JavaScript? ...

implementing multiple fields in jquery ui autocomplete

I have encountered an issue with the code where the second input field is not displaying images along with text suggestions. Can someone please review the JavaScript and suggest any necessary changes to make it work correctly? Example queries: clinton, bu ...

Having trouble executing my Node.js project written in Typescript due to an error: TypeError [ERR_UNKNOWN_FILE_EXTENSION] - the file extension ".ts" for /app/src/App.ts is unrecognized

After attempting to launch my application on Heroku, I encountered the following stack trace. The app is a basic ts.app using ts-node and nodemon. I am eagerly awaiting the solution to this issue. 2020-05-30T00:03:12.201106+00:00 heroku[web.1]: Starting p ...

Identifying the conclusion of a lengthy request in NodeJS/Express

Currently grappling with Server-sent events, my pseudo-code setup involves the following: var listeners = []; app.get('/stream', function (req, res) { listeners.push({ req: req, res: res }); }); // ... some action takes place notify(listene ...