Leveraging Angular2+ components across various modules

Bringing in a component called "TemperatureComponent" into a module named "DashboardModule" and defining + exporting it there:

import { TemperatureComponent } from './temperature/temperature.component';
import { TemperatureDraggerComponent } from './temperature/temperature-dragger/temperature-dragger.component';
//...

@NgModule({
  imports: [
    //...
  ],
  declarations: [
    //...
    TemperatureDraggerComponent,
    TemperatureComponent,
  ],
  exports: [
      TemperatureComponent,
      TemperatureDraggerComponent,
  ]
})
export class DashboardModule { }

Functioning correctly within that module. I'm attempting to utilize the same component in another module by using its selector, (the selector also functions properly in DashboardModule). I attempt to achieve this by importing DashboardModule:


import { ThemeModule } from '../../@theme/theme.module';
import { DashboardModule } from '../dashboard/dashboard.module';

@NgModule({
  imports: [
    //...
    DashboardModule,
  ],
})
export class DiceModule { }

In the HTML of this second module, the selector is as follows:

 <div class="col-xxxl-3 col-md-6" *ngFor="let statusCard of statusCards">
    <ngx-status-card [title]="statusCard.title" [type]="statusCard.type">
      <i [ngClass]="statusCard.iconClass"></i>
    </ngx-status-card>
  </div>
</div>

<div class="row">
  <div class="col-xxxl-3 col-xxl-4 col-lg-5 col-md-6">
    <ngx-temperature></ngx-temperature>
  </div>
</div>

This results in the error:

ERROR in src/app/pages/dice/dice.component.html:11:5 - error NG8001: 'ngx-temperature' is not a known element:

  1. If 'ngx-temperature' is an Angular component, then verify that it is part of this module.
  2. If 'ngx-temperature' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

Additional code provided upon request:

temperature.component.ts

 
// Code for TemperatureComponent here...

temperature.component.html

 
// HTML template code for TemperatureComponent here...

Summary: Attempting to share a component between Modules A & B by:

  1. Importing, declaring & exporting it in Module A.
  2. Importing Module A into Module B.

Questioning why the selector isn't functioning and instead showing an error?

P.S: Both modules are imported into a parent module named Pages. Unsure if this is best practice or if it is relevant in any way.

Answer №1

I encountered an issue where the component (DiceComponent) was not being imported into the second module (DiceModule). To resolve this, I made sure to import it into the module by adding the necessary line of code:

import {
  NbActionsModule,
  NbButtonModule,
  NbCardModule,
  NbTabsetModule,
  NbUserModule,
  NbRadioModule,
  NbSelectModule,
  NbListModule,
  NbIconModule,
} from '@nebular/theme';
import { NgxEchartsModule } from 'ngx-echarts';

import { ThemeModule } from '../../@theme/theme.module';
import { FormsModule } from '@angular/forms';
import { DiceComponent } from './dice.component'; //<-- This line was mistakenly omitted.
import { DashboardModule } from '../dashboard/dashboard.module';

@NgModule({
  imports: [
    FormsModule,
    ThemeModule,
    NbCardModule,
    NbUserModule,
    NbButtonModule,
    NbTabsetModule,
    NbActionsModule,
    NbRadioModule,
    NbSelectModule,
    NbListModule,
    NbIconModule,
    NbButtonModule,
    NgxEchartsModule,
    DashboardModule,
  ],
  declarations: [
    DiceComponent, // <-- This line ensures the component is included.
  ],
})
export class DiceModule { }

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

Encountering a Schema error when using Components in an Angular 7 Element

I have been working on integrating various external libraries into my custom component to use in a dashboard project I'm developing. To simplify the process, I decided to create an Angular Element that includes a Line Chart, Graphic Gauge, and additio ...

In TypeScript, the 'as const' syntax results in a syntax error due to the missing semicolon

I incorporated the following code into my react project: export const animation = { WAVES: "waves", PULSE: "pulse", } as const; export type Animation = typeof animation[keyof typeof animation]; Upon running the project, I encounte ...

angular Issue with setTimeOut() function malfunctioning

I have a div with the class alert. <div class="alert" *ngIf=alert> <h1>Copied</h1> </div> I am trying to display this div for 3 seconds. Initially, it will be hidden and after clicking a button, I run a function that cha ...

Using the angular2-cookie library in an Angular 2 project built on the rc5 version

Starting a new angular2 rc5 project, I wanted to import the angular2 cookie module. After installing the module with npm, I made changes to my angular-cli-build.js file : npm install angular2-cookie edited my angular-cli-build.js file : module.exports ...

What methods are most effective for integrating a Loading indicator into a redux-saga application?

Currently, my app is being developed using a combination of redux, redux-saga, typescript, typesafe-actions, and ducks. I am exploring different ways to incorporate a loading indicator while making API calls. This is the structure I have in place: sta ...

What is the best way to modify the quantity property of a cart item object within the framework of this Angular 13 online shopping application?

I am currently developing an e-commerce app with a front-end built using Angular 13. The following code snippet is designed to calculate the total price of items in the shopping cart: import { Component, OnInit } from '@angular/core'; @Componen ...

Using discord.js to conveniently set up a guild along with channels that are equipped with custom

When Discord devs introduced this feature, I can't seem to wrap my head around how they intended Discord.GuildManager#create to function. How could they possibly have expected it to work with Discord.GuildCreateOptions#channels[0], for instance, { ...

Angular Material's <mat-select> element can trigger a function to execute when it loses focus

I'm trying to add functionality to a <mat-select> element so that a function is executed when the element loses focus. Currently, the function only executes on selection change: <mat-form-field fxFlex> <mat-label>SSR Status</m ...

When working with async functions in JavaScript using await, the second function may not necessarily wait for the first function to complete before executing again

My goal is to implement async await in Angular 10 for loading a list of users from the backend database (built with Spring Boot and MariaDB) using an http request, and then filtering that list for one specific user. However, I'm facing an issue where ...

error encountered with lazy loading while interacting with modal components

Hey there! I'm currently working on implementing lazy loading with a modal component. Here's how my shared components module looks like: @NgModule({ declarations: [ AddNoteComponent, EditNoteComponent ], imports: [ IonicModu ...

Decipher intricate JSON data using Typescript

When a request receives a response, it is delivered in JSON format. The specific structure of the JSON data is as follows: { "32": { "docKey": "32", "outletId": 32, "mdngOutlet": { "outletBasic": { "outletId": 32, } ...

Transmitting a variety of content types in a single request

Is there a way to send multiple files along with extra JSON data to the API? I have noticed that the API is capable of handling various content types. I am wondering how I can create a header that includes two different content types: Multipart form data ...

What is the TypeScript alternative for `const x = require("somemod")();`?

When working with node.js in my TypeScript project, I'm incorporating jsreport-core. In their code, they import it using var jsreport = require('jsreport-core')(); with the trailing (). I'm interested in finding out the most effective w ...

Struggling to retrieve the HTTP Response Code from an Angular Service

In my current project, I am utilizing Angular 4 for frontend development and Django REST Framework (DRF) as the backend. The issue I am facing is with extracting the Response JSON and HTTP Response Code separately from the DRF end in the form of a response ...

typescript encounters issues with union type while trying to access object properties

I'm puzzled by the errors I'm encountering in my IDE with the following code: I defined some interfaces/types interfaces/types: interface GradientColor { type: string; value: { angle: string | number; colours: string[]; }; } inte ...

Contrasting type[] (array) with [type] (tuple)

If we imagine having two interfaces: interface WithStringArray1 { property: [string] } interface WithStringArray2 { property: string[] } Let's define variables of these types: let type1:WithStringArray1 = { property: [] } let type2:Wit ...

Turn off the page refresh in Angular 6 routing

Having an issue with Angular where clicking a link causes the browser to reload the entire site instead of just loading the component associated with the path. What should I do? The default components are Page1 and Page2. In app.module.ts: import {Router ...

The protractor tool is unable to recognize an Angular component when it is displayed on the page

I have implemented end-to-end (e2e) testing on my project, but I am facing issues that are causing my tests to fail. Below is a snippet of my app.po.ts file: import { browser, by, element } from 'protractor'; export class AppPage { public b ...

What is the best way to grab an uploaded file in Angular, similar to how we capture the values of text boxes?

The application I am working on consists of five pages. Users are required to upload a file, and that same file needs to be displayed on other pages when the user loads them. Is there a way to achieve this functionality using Angular? ...

(Vue with Typescript) Error: Component cannot be mounted as template or render function is not defined

Having trouble mounting component due to template or render function not defined. Hello everyone, I'm currently attempting to incorporate Typescript into my Laravel / Vue project. However, I've been encountering issues such as the following erro ...