Creating a global component in Angular 2: A step-by-step guide

I attempted to create a component with global scope, but encountered an error:

Uncaught Error: Can't export directive LanguageComponent from SharedModule as it was neither declared nor imported!

To address this, I included the component in ShareModule like this:

exports: [
    LoaderComponent,
    LanguageComponent
  ]

I also tried adding the component to core.module as a declaration, but that did not resolve the issue.

ShareModule structure:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoaderComponent } from './loader/loader.component';
import {LanguageComponent} from "../language/language.component";
import {HiderPipe} from "../pipes/Hider";

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    LanguageComponent,
    LoaderComponent,
    HiderPipe
  ],
  exports: [
    LoaderComponent,
    LanguageComponent,
    HiderPipe

  ]
})
export class SharedModule { }

This module is only imported in the main core.module like so:

@NgModule({
  imports: [

    SharedModule
  ],

Answer №1

To ensure a component is accessible to the module importing the SharedModule, you must include the LanguageComponent in both the declarations and exports fields of your SharedModule.

@NgModule({
  imports: [
    // ...
  ],
  declarations: [
    // ...
    LanguageComponent
  ],
  exports: [
    // ...
    LanguageComponent
  ]
})
export class SharedModule {
  // ...
}

If you encounter an error indicating that the component was not declared or imported, make sure it is added to the declarations field:

Uncaught Error: Can't export directive LanguageComponent from SharedModule as it was neither declared nor imported

If the LanguageComponent is part of another module (e.g., LanguageModule), import that module and reexport it if needed.


To use the component in a different module, such as AppModule, add the SharedModule to the imports array of that module.

@NgModule({
  imports: [
    // ...
    SharedModule
  ],
  declarations: [
    // ...
  ],
  exports: [
    // ...
  ]
})
export class AppModule {
  // ...
}

Then, utilize the component in your template using its selector. For more information, refer to the Feature Module section in the documentation.

Answer №2

Another key step is incorporating the declarations array.

declarations:[LanguageComponent]

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

Sending a JSON payload from Angular to C# can result in a value of 0 or null being received

As a beginner working on my Angular&C# project, I encountered an issue. Despite sending a JSON from Angular to C# (POST) with values, the C# side is not receiving anything (value 0 for int and null for string). I can't figure out what I'm doi ...

Utilizing a string variable as a property name for dynamic typing

I am looking to dynamically create a type with a property name based on specified parameters. Although I can successfully create the object, I am facing challenges when trying to create the actual type. This dynamic type creation is essential for compositi ...

Error in redirection while deploying Auth.js (v5) within a Docker container in a Next.js application

Has anyone successfully integrated the latest version of Auth.js into a production environment with Docker? I am currently utilizing the t3-stack (tRPC, Auth.JS, Prisma, Next.JS). I attempted to upgrade to the beta version with the Prisma Adapter, but enc ...

Mistakenly importing the incorrect version of Angular

While working on my Angular 1 app in typescript, I faced an issue when importing angular using the following syntax: import * as angular from 'angular'; Instead of importing angular from angular, it was being imported from angular-mocks. Thi ...

Is there a method to increase the level of generality of an object containing generic functions?

Here is a unique question that delves into the nuances of type manipulation in TypeScript. Rather than focusing on obtaining return values of generic functions, this query explores transforming a specific type Have into another type Want<T>. Imagine ...

What are some best practices for integrating ES2020 into an Angular project?

Below is the content of my tsconfig.json file: { "compileOnSave": false, "compilerOptions": { "baseUrl": "./", "outDir": "./dist/out-tsc", "sourceMap&q ...

Error encountered while using Jest, React, Typescript, and React-Testing-Library

I have set up a project using React/NextJS with Typescript and now I am incorporating unit testing with Jest and React Testing Library. One of the unit tests for my component is as follows: import React from 'react'; import '@testing-libra ...

Is there a way to have my accordion adjust automatically?

I have developed a dynamic accordion component that populates its values from the parent component. However, I am facing an issue where each accordion does not respond individually to clicks. Whenever I click on any accordion, only the first one expands an ...

Can an entire object be bound to a model in an Angular controller function?

In TypeScript, I have defined the following Interface: interface Person { Id: number; FirstName: string; LastName: string; Age: number; } Within a .html partial file, there is an Angular directive ng-submit="submit()" on a form element. A ...

Angular: Error when TypeScript object returns an array object value

I have encountered a strange issue where the array value returned as [object Set] when I console log it. It's unclear whether this problem is occurring in the component or the service, but the object values are not being displayed. This issue arises ...

Storing user information in local storage with the Capacitor Storage Plugin: A Comprehensive Guide

I'm attempting to integrate Firebase Authentication into my Angular application. Here's the signUp() function within my AuthService: signUp(email: string, password: string, name: string) { const userCredential = from( firebase.auth(). ...

Microsoft Edge introduces an unexpected parameter to the URL

Unusual parameter ?zx=djlj8eui511x appearing in URL after clicking <a [href]="navItem.url" *ngIf="navItem.index <= wizardConfiguration.activeTab.index" class="rktn-wizard-navigation-container_passed-item" ...

When a webpage reload triggers a 404 error, my website's iframe with the source "videoUrl | sanitize" will display

I am attempting to retrieve a videoUrl from a database and set it to the iframe [attr.src] in order to display a YouTube video. It is imperative that the data originates from the database, as there are limitations preventing us from directly uploading and ...

Chart of commitments and potential outcomes

I am in the early stages of learning about promises and I am struggling to understand how to write code correctly. Here is an overview of what the program should do: Retrieve a list of item types (obtained through a promise) Loop through each item type to ...

`Angular 10 project fails to include JWT refresh token in cross-origin requests`

Our development setup includes an Angular front end and a web service running on a different port on the same machine. The web service is configured to allow access from localhost and the port hosting the Angular application. We are utilizing JWT authenti ...

Is it possible to convert a DynamoDB AttributeMap type into an interface?

Assume I define a TypeScript interface like this: interface IPerson { id: string, name: string } If I perform a table scan on the 'persons' table in DynamoDB, my goal is to achieve the following: const client = new AWS.DynamoDB.Documen ...

Tips for attaching to a library function (such as Golden Layout) and invoking extra functionalities

I am currently utilizing a library named Golden Layout that includes a function called destroy, which closes all application windows on window close or refresh. My requirement is to enhance the functionality of the destroy function by also removing all lo ...

Struggling to accurately capture the values from checkboxes and dropdown selections to ensure the correct data is displayed. Assistance is needed in this

I am facing challenges in retrieving the accurate data for display from Mat-Select and Mat-Checkbox components. My goal is to capture the selected values from users and perform if-else statements to validate conditions, displaying the correct data view if ...

SonarQube alerting you to "Eliminate this unnecessary casting"

Can someone help me understand why SonarQube is flagging this error and suggest a resolution? The unnecessary cast should be removed. Promise.all([ this.customerViewCmr4tProvider.getData(activeNumber), this.customerBillManagementProvider.getData(ind ...

Building a multi-panel electron app with React and Angular

Is it possible to create a multi-window Electron application with React/Angular, where each window is powered by its own HTML file? In traditional React/Angular applications, the build process typically results in a single HTML file (e.g., index.html). Ho ...