What is the method to access a component from a module that has been imported into the app module?

We are currently working on a project that has the following hierarchy.

app/
├── app.component.html
├── app.component.ts
├── app.module.ts    <--moduleA and moduleB is imported here  
├── app-routing.module.ts
├── moduleA/
│   ├── moduleA-routing.module.ts
│   ├── moduleA.module.spec.ts
│   ├── moduleA.module.ts
│   ├── moduleA.component.ts
│   └── moduleA.component.html
├── moduleB/
│   ├── moduleB-routing.module.ts
│   ├── moduleB.module.ts     <-- moduleC is imported here
│   ├── moduleB.component.module.ts
│   ├── moduleB.component.html
│   ├── moduleB.component.ts
|   ├── moduleC/
│   │    ├── moduleC-routing.module.ts
│   │    ├── moduleC.module.ts
│   │    ├── moduleC.component.module.ts
│   │    ├── moduleC.component.html   --> Need to use moduleA component here
│   │    └── moduleC.component.ts

moduleC is a sub-module of moduleB. We have already imported moduleA in AppModule. The question arises - can we access components of moduleA in moduleC without reimporting moduleA within moduleC? In simpler terms, do we need to import a module that has already been included in AppModule again in its child module just to utilize its components?.

When we attempted to do so without importing moduleA again in moduleC, an error was thrown:

"Template parse errors: {{selector name}} is not a known element"

However, upon importing it again, everything worked as expected.

Answer №1

Avoid importing moduleA and moduleB directly into the app.module.ts file.
Instead, import moduleA within moduleB.module.ts
Then import moduleB in moduleC.module.ts
Lastly, import moduleC into the app.module.ts file
This allows you to access the moduleA component within both moduleB and moduleC.

Answer №2

To gain access to ModuleA from an external module, it is recommended to use imports:[ModuleA] and exports: [ModuleA]

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

Tips for sending a file rather than a json object in nextjs

Is there a way to send a file from either route.ts or page.ts, regardless of its location in the file-system? Currently, I am using the following code in my back-end python + flask... @app.route("/thumbnail/<string:filename>") def get_file ...

Unable to iterate over a JSON response from a POST request in Angular 8

I am attempting to iterate through a JSON object that is returned after making a POST request to my REST API. However, I'm encountering the following error: DatetodateComponent.html:33 ERROR Error: Cannot find a differ supporting object '[objec ...

Is it Possible for the Number Array Type to Not Be Recognized as an Array?

export class ... { single: any[] = []; multi: any[] = []; view: number[] = [700, 400]; ... <Removed for brevity> } Error Message: It says 'Type 'number[]' is not assignable to t ...

The current directory does not belong to a Cordova project

Upon executing ionic cordova run browser --verbose within the main directory of my Ionic4 project, I encounter the error message "Current working directory is not a Cordova-based project." as shown below. I've observed that the command generates a "w ...

Learn how to host a singular instance of an Angular application for every unique route, similar to the setup utilized in meet.jit.si

Is there a way to create an Angular app that loads a new instance of itself on every route/url? For example, visiting http://happy-app.com/happy would show the app in one state, and going to http://happy-app.com/happier would load the same app in a differe ...

Automating the scrolling function in Angular 2 to automatically navigate to the bottom of the page whenever a message is sent or opened

In my message conversation section, I want to ensure that the scroll is always at the bottom. When the page is reopened, the last message should be displayed first. HTML: <ul> <li *ngFor="let reply of message_show.messages"> ...

Using Angular2 moment to format a date in a specific way

Encountered an error while using the amTimeAgo pipe from angular2-moment. The error message is as follows: Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not ...

Angular2 - Error: The view has been destroyed and cannot be updated: detectChanges

My application keeps encountering this persistent error: extensions::uncaught_exception_handler:8 Error in event handler for runtime.onMessage: Attempt to use a destroyed view: detectChanges at ViewDestroyedException.BaseException [as constructor] (chrome ...

Sanity.io and using images with next/image extension glitch

Hello everyone, I'm excited to join Stack Overflow for the first time. Currently, I am working on a project using Next.js with Sanity as my headless CMS. I have come across what appears to be a TypeScript type issue. Here is the link to the code on Gi ...

Is it possible for NgElse to target an imported component?

I'm in the process of creating a universal loading icon for my website. Here's what I have so far. <div> <p *ngIf="questionsLoaded; else loadingSign" > {{lstQuestions?.length}} question/s found </p> <ng-template #l ...

Join the Observable and formControl in Angular 4 by subscribing

My goal is to display the data retrieved from FireStore in the screen fields upon loading. However, the buildForm() function is being called before subscribing to the data, resulting in the failure to populate the screen fields with the FireStore data. pe ...

What is the issue with this asynchronous function?

async getListOfFiles(){ if(this.service.wd == '') { await getBasic((this.service.wd)); } else { await getBasic(('/'+this.service.wd)); } this.files = await JSON.parse(localStorage.getItem('FILENAMES')); var ...

Tips for creating an Angular testing scenario that encompasses the use of array.find()

I need assistance in writing a test case for the following method. I have created the code snippet below but it is not helping to improve/change the code coverage. Can anyone please guide me on what I might be doing incorrectly? Component remove(item: str ...

Angular - Expanding a d3 SVG element generated dynamically in the code

Welcome to my first question on this platform. If I make any mistakes, please forgive me. I am currently working on creating a simple SVG in Angular using the d3 library, but I am struggling to make it scale properly. After doing extensive research on SV ...

Encountering the error message "Error: 'preserveValueImports' is an unknown compiler option" while setting up a SvelteKit project

https://i.stack.imgur.com/fnidk.png Every time I set up a new sveltekit project using TypeScript, I keep encountering the error "Unknown compiler option 'preserveValueImports'.ts" in the tsconfig.json file. The error shows up above the line wher ...

Chai is unable to differentiate between different class types

When using Chai to compare if a returned value of type SimpleModel matches with the expected type SimpleModel, I encountered an error even though my IDE indicated that the types are correct: AssertionError: expected {} to be a simplemodel The setup for t ...

Incorrect date being sent by Mat Date picker

I am encountering an issue with date selection using this input field <mat-form-field class="w-full"> <mat-label>{{ "DATE" | translate }}</mat-label> < ...

Retrieve information from the API following a change in language with Ngx-Translate

I am working on a web app using Angular 12 that supports 3 languages. To manage the languages, I have utilized Ngx-Translate. Overall, everything is functioning correctly, but I have encountered a small issue. I am using the onLangChange event emitter to ...

Developing custom events in an NPM package

Developing a basic npm package with signalr integration has been my recent project. Here's how it works: First, the user installs the package Then, the package establishes a connection using signalr At a certain point, the server triggers a function ...

A dynamic Angular component consisting of two distinct rows to be seamlessly integrated into an existing table

I'm currently working on a component called "template" that can consist of either two rows. @Component({ selector: '[my-row]', template: ` <tr> <td>first</td> <td>second</td> </tr> <t ...