Importing Modules in Angular: Explicit vs Implicit Approach

My current setup includes a SharedModule that is imported by every other module. This means that some modules have an implicit import to the SharedModule because they import other modules that already import it. I'm curious if this could potentially impact the bundle size, or if Ivy handles this situation seamlessly without any issues?

https://i.sstatic.net/vh2gP.png

Answer №1

According to the information provided in the Angular documentation:

Is it a problem if I happen to import the same module more than once?

No, importing the same module multiple times is not an issue in Angular. When multiple modules import Module 'A', Angular only evaluates Module 'A' once, the first time it encounters it, and does not repeat the evaluation process.

This holds true regardless of where Module 'A' appears within a hierarchy of imported NgModules. For example, if Module 'B' imports Module 'A', Module 'C' imports 'B', and Module 'D' imports [C, B, A], then when 'D' is processed, Angular evaluates 'C', which triggers the evaluation of 'B', and subsequently 'A' is evaluated. By the time Angular reaches 'B' and 'A' in 'D', they are already cached and ready for use.

Angular does caution against NgModules with circular references, so it is best to avoid scenarios where Module 'A' imports Module 'B', which in turn imports Module 'A'.

For further information, you can visit: https://angular.io/guide/ngmodule-faq#what-if-i-import-the-same-module-twice

Answer №2

Rest assured, Angular ensures that only one instance of your module is created, even if multiple modules import your SharedModule.

When using LazyLoading, instances will not be created until the LazyLoaded module is loaded first.

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

Is there a way to disable the automatic refreshing of the useForm feature?

Upon clicking submit, all field information is supposed to be sent to the backend, but instead, it is being appended to the browser's URL. Furthermore, the error messages from yup are not being displayed. I attempted to use event.preventDefault in ha ...

Unable to modify the date format in Ionic 2 once it has been initially selected by the user in the date

In this snippet of HTML, there are two elements - a button and a date input. <ion-datetime id="datetime-12-0" pickerFormat="DD/MMM/YY" min="2017" max="2020" [(ngModel)]="date.date"></ion-datetime> <button (click)="triggerClick('dat ...

"Uploading user profile images with Angular, Express, Multer, and Mongoose made easy

Hey there, I'm currently using multer to upload images. When I select an image, it gets saved in the backend folder called uploads. However, I would like to store it in a MongoDB database and then display that image on the frontend using Angular. I&ap ...

Interface specifying a React.ref property

When attempting to use a string as the ref, I encountered a warning when trying to access ref?.current Property 'current' does not exist on type 'string' What should be used for the ref prop in the interface? I am uncertain of the upfr ...

Exploring methods for interacting with and controlling structural directives in e2e testing

Background: My goal is to permutation all potential configurations of an Angular2 screen for a specified route and capture screenshots using Protractor from the following link: http://www.protractortest.org/#/debugging. Problem: I am struggling to figure ...

Instructions on how to deactivate or unselect a toggle switch by simply clicking on the icon among several icons

In my angular application, there are several icons with a toggle switch on the right side. The default state of the switch is ON. When an icon is clicked, its color changes from white to red. By clicking on the switch, the colored icon reverts back to whit ...

The 'ngForOf' property cannot be bound to 'tr' as it is not recognized. Make sure to import CommonModule and BrowserModule

Encountering a strange issue. When attempting to use *ngFor on a tr element, an error is thrown: <table> <tbody> <tr *ngFor="let item of productsData"> <!-- some Code --> <tr> </tbody> </table> Within my ...

What is your approach to organizing the HTTP requests in your Angular 2+ application?

I'm looking for suggestions on how to better organize my API calls within my services. How have you structured your API calls? Do you centralize all API calls for the entire application in one dedicated service? Do you create an HTTP folder and sepa ...

Can a default arrow function be exported in TypeScript using only one line?

const myFunction: () => void = () => { console.log('I am able to export my function like this'); }; export default myFunction; export default () => void = () => { console.log('I am unable to export my function like thi ...

Define the state of an object using Parent and Children classes following the application of a filter

Within Angular 8, I am dealing with an Observable: let parents: Observable<Parent[]>; The classes Parent and Child are defined as follows: class Parent { id: number; name: string; children: Child[]; } class Child { id: number; name: str ...

Determining the Best Option: React JS vs Angular UI Framework

After researching the latest versions of React and Angular online, I discovered that both are suitable for developing Web Application UI. However, I also realized that there are key differences to consider when choosing between the two. Let's say I h ...

Experiencing a compilation issue while attempting to apply the class-transformer

Encountering an issue while working with a basic example that involves class-transformer. error TS1240: Unable to resolve signature of property decorator when called as an expression. Argument of type 'ClassFieldDecoratorContext<Root, Project[]> ...

How to Install Definitely Typed Packages in Visual Studio 2015 RC for AspNet5 with the Help of Gulp and TSD

Struggling to add angular.d.ts to my MVC6 project, I've hit a roadblock. Although I've configured Gulp to reinstall necessary packages using tsd, I'm stuck on the initial installation process. The instructions suggest running 'tsd inst ...

Utilize Angular6 and NodeJS to exhibit images stored on a server file system

Successfully uploaded images to a server but struggling to find a tutorial on how to display these images using Angular6 and NodeJS. Any help would be greatly appreciated. Thank you in advance. Edit: After many trials and errors, I was able to retrieve a ...

Unable to implement client-side functionalities in Angular 17 with server-side rendering

view image description hereview image description here Is there a way to make my component run on the client side, similar to "use client" in Next.js? This is my first time working with server-side rendering in Angular, and everything works fine when it&a ...

Angular2: How to deactivate a specific element in a form created with formbuilder

After initializing my formbuilder, I need to disable a specific element because I need to perform some validation once the view is loaded. Here is how I declared my formBuilder. ionViewDidLoad() { this.purchaseDataForm = this.formBuilder.group({ kms ...

Vitest encountered an issue fetching a local file

I am currently working on testing the retrieval of a local file. Within my project, there exists a YAML configuration file. During production, the filename may be altered as it is received via a web socket. The code functions properly in production, but ...

platform designed for handling various fields of identical types

I am dealing with multiple transactions that I intentionally do not want to group into an array. Instead, I prefer to separate them as individual fields. For example: { ['fixtureId']: { transactionId: string; donation: boolea ...

Setting default values and specifying available values for a class property in TypeScript

I'm curious about how to set a default value for a class property, as well as define all available values at once. For example: class MyClass{ isActive = -1; //Setting default value } class MyClass{ isActive: -1 | 0 | 1; //Defining all available ...

Combine TypeScript files in a specific sequence following compilation

I am hoping to utilize gulp for the following tasks: Compiling TypeScript to JavaScript, which is easily achievable Concatenating JavaScript files in a specific order, proving to be challenging Since I am developing an Angular application, it is crucial ...