Is it necessary to include explicit overlapping imports in Angular if they are already imported in the parent component?

Do you think the title needs clarification? Feel free to offer suggestions.

I've noticed that my design components are ending up with a lot of imports. Some of these imports are duplicated in the component that is importing them. I'm managing to make it work but there's a nagging feeling of code smell creeping in.

Let's say we want to give a monkey a banana.

import { Banana } from "./banana";
@Component
export class Monkey { }

Now, let's say some guy wants to own a monkey.

import { Monkey } from "./monkey";
@Component
export class Dude { }

But what if this guy is also hungry and wants a banana for himself?

import { Monkey } from "./monkey";
import { Banana } from "./banana";
@Component
export class Dude { }

This situation seems inefficient and redundant to me. The second import seems unnecessary since the guy already has access to a banana through the monkey.

Is this the proper way to structure import hierarchy in Angular? Or is there a more efficient way to reduce the amount of scrolling required when dealing with numerous imports in each file?

If there isn't a better solution, I would love to hear the reasoning behind this practice as a point of reference.

Answer №1

To make identifiers available from multiple files, you can export from other files by using a single import statement:

import { Monkey } from "./monkey";
export { Monkey } from "./monkey"; // re-export
@Component
export class Dude { }

For more information, refer to https://angular.io/docs/ts/latest/glossary.html#!#B

However, be cautious as this approach can result in difficult-to-locate errors if there are cycles present. Check out Angular 2 DI Error - EXCEPTION: Can't resolve all parameters

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

Error SCRIPT1002 was encountered in the vendor.js file while using Angular 8 on Internet Explorer 11

Having trouble getting Angular to function properly in IE 11. I've tried all the solutions I could find online. The errors I'm encountering are as follows: SCRIPT1002: Syntax error File: vendor.js, Line: 110874, Column: 40 At line 110874 args[ ...

What is the process for importing a component at a later time?

I am attempting to import components with a delay in a seamless manner. My goal is to import the components discreetly so that they load smoothly in the background while viewing the homepage. I experimented with lazy loading, but found that it caused dela ...

Encountering an issue with reading the property 'hasError' of undefined in reactive nested forms

I recently implemented reactive nested forms using Angular 8 along with Angular Material. Inside the component.ts file: this.dataForm = this.formBuilder.group({ name: [null, Validators.required], user: this.formBuilder.group({ firstnam ...

The request for XMLHttpRequest has been restricted from accessing ASP.NET CORE 2.2.0 with Angular 8 and signalr1.0.0 due to a failure in the CORS policy (Access-Control-Allow-Origin)

nugetPackage on .net core2.2.0: signalr 1.0.0 + ASP.Core2.2.0 I am utilizing angular to utilize signalr: package.json: "@aspnet/signalr": "1.1.0", my front-end Angular code: import { Component } from '@angular/core'; import * as signalR fro ...

Is it possible for me to use ts files just like I use js files in the same manner?

So I recently stumbled upon TypeScript and found it intriguing, especially since I enjoy adding annotations in my code. The only downside is that I would have to change all my .js files to .ts files in order to fully utilize TypeScript's capabilities. ...

Ways to manipulate the placement of angular material 2 sidenav content to the bottom

I have been experimenting with various methods in CSS to override the existing side nav component in Angular Material 2. mat-sidenav-container { background: white; bottom: 0px !important; /* <---- no success */ } mat-sidenav { width: 300px; ...

Embedding Globalize.js into an Angular component

Hey there! I'm currently working on building an Angular 4 application that needs to support L10n. I've decided to incorporate globalize into my project. Below is a snippet of my App component: import { Component, OnInit } from '@angular/c ...

Ionic has been experiencing issues with inaccurate boolean results being generated by Angular typescript

I have created a random number generator that requires the user to input a maximum and minimum value to generate a random number within that range. The application will show an error under two conditions: If either of the numbers entered is negative If t ...

retrieve the key associated with a translated value using ngx-translate

Using @ngx-translate along with localize-router has posed a challenge for me. I am unable to resolve a valid slug from a localized URL. For example, if the translation of 'about' is 'asd', then: routerLink="about/{{'about' ...

Filtering Deno tests by filename: A step-by-step guide

How can I selectively run Deno tests based on their filenames? Consider the following test files: number_1_test.ts number_2_test.ts string_test.ts If I want to only run tests with filenames starting with number*, I am unable to use either of these comma ...

Feeling lost about arrow functions in JavaScript

Here is the code I am currently using to increment the value of intVariable using window.setInterval. var Arrow = (function () { function Arrow() { this.intVariable = 1; this.itemId = -1; this.interval = 25; } Arrow.p ...

Implementing Angular 2 reactive forms checkbox validation in an Ionic application

I have implemented Angular Forms to create a basic form with fields for email, password, and a checkbox for Terms&Conditions in my Ionic application. Here is the HTML code: <form [formGroup]="registerForm" (ngSubmit)="register()" class="center"> ...

Understanding the integration of sass with webpack in an Angular 4 project

Is it possible to reference a sass file instead of a css file directly in the index.html? If so, how does webpack compile the sass into a css file? Additionally, what is the most effective way to bundle the sass file when building the application? The ve ...

How to Retrieve Inputs from Child Component Form without Prop Passing?

Within the Parent component below, there is a Dropdown menu with two options. Selecting "TOP LEVEL" will display Form1, while selecting "MAKE ITEM" will show Form2. If no option is selected, both forms remain hidden. The Parent component also contains a Bu ...

Conditional type in Typescript can be used to infer tuple types

Why are output1 and output2 not both inferred as [number, number, number] in the following code snippets? Snippet 1 : type InferTuple1<T> = T extends any[] ? [...T]: never; const func1 = <T>(t: InferTuple1<T>) => t; const output1 = ...

Using `querySelector` in TypeScript with Vue 3

Encountered a TypeScript error while using the Vue Composition API let myElement = document.querySelectorAll('my-element') The TS error I'm getting when trying to access it like this: Property '_props' does not exist on type ...

Understanding Scope in TypeScript

Recently, I developed a sample application in Node.js which utilizes pg-promise to execute queries on a Postgres database. I encapsulated this functionality within a class named PostgresDataAccess. However, I encountered an issue while trying to access t ...

What is the process for defining the default landing page route in Angular routing?

My application currently has only one route, and I want it to start with the URL http://localhost:4200/specialtyQ. However, my application is not loading properly. The code snippet below is what I am using to try to achieve this. How can I correct the URL ...

Can Angular 9 be used to compile a latex document?

Is it possible to utilize Angular 9 to compile and generate PDF files using latex? Specifically, I am curious about how to compile a document using Angular and Pdflatex. The idea is for the client to input their data in the form of a JSON data structure ...

Problems arising from positioning elements with CSS and organizing list items

I've been working on creating a navigation sidebar using Angular and Bootstrap, but I'm having trouble getting my navigation items to display correctly. APP Component HTML <div class="container-fluid"> <div class="row" id="header"&g ...