Tips for dynamically importing parent services

Is it possible to dynamically import service-A (85KB) into service-B (15KB) and then dynamically import service-B into app.comp.ts when needed?

Check out the Stackblitz Demo here

View the FlowChart here

Answer №1

Utilizing angular DI in this scenario can be quite beneficial. It is advisable not to directly import your services like so:

export class MainComponent {
  constructor(private injector: Injector) {}
  
  async fetchService() {
    const {DataService} = await import('./utility.service');
    const service = this.injector.get(DataService);
    service.performAction();
  }
}

The reason why this approach works is because DataService is provided through the global injector with provideIn: 'root'. Consequently, we have the ability to fetch the service, followed by instantiating it from the injector.

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

Pattern that incorporates a numerical value ranging from 0 to 99, while also permitting a decimal point with up to two digits

When developing an Angular app, we are in need of validation that allows users to enter values between 0 and 99, with the option of up to two decimal points. Here are some examples: 0 -> Pass 0.1 -> Pass 1.12 -> Pass 12.12 -> Pass 1 ...

Toggle divs by using a checkbox (Vue.js)

On my authentication page, I have implemented a checkbox that I want to use to toggle between Sign Up and Sign In forms. When the checkbox is clicked, it should display the Sign Up form, and when it's unchecked, it should show the Sign In form. I fou ...

Can I programmatically retrieve a comprehensive list of all global HTML attributes available?

Exploring the extensive list of global HTML attributes can be overwhelming, especially when considering how it continues to evolve with browser updates. Can JavaScript be leveraged to dynamically generate a complete list of these attributes without the ne ...

NgrxStore - An initial item has been added twice to the array

Currently experimenting with ngrx store and manipulating elements within an array, such as deleting, fetching, and editing, works smoothly. However, a challenge arises when inserting an object into the array for the first time, duplicating the entry unless ...

Receiving a getRange error when importing JSON into Google Sheets

Struggling with importing JSON data to Google Spreadsheets using Google Script? I don't have much Java knowledge, so forgive me if this question seems a bit silly! However, I do have experience coding in VBA. My current challenge involves pulling liv ...

Conceal a Component within an Embedded Frame

Hey there! I've been attempting to hide a header within an iframe, but it seems like my code isn't doing the trick. Could someone take a look and help me diagnose why the header is still visible? Thanks in advance! <iframe id="booking_iframe" ...

Experimenting with Jest testing for a Component or Service that includes the use of "declare var" functionality

During the testing of my component or service, an error has occurred: ReferenceError: flagPath is not defined The variable flagPath is sourced from a configuration file named config.js within the assets folder. Is there a method to incorporate it into ...

Separate the Array elements and organize them into an object

Check out this code snippet that demonstrates how to divide an array into chunks. If you have a more efficient solution, please share it with me. var numbers = []; for (var i = 0; i < 4500; i++) { numbers.push(i); } var chunks = {}; var start = 0 ...

Retrieve the key values from an object of a generic type

Is there a way to retrieve the keys of the object when it is of type T? I attempted to accomplish this using different methods such as: function getGenericTypeKeys<T>(): string[] { return Object.keys({} as T); } and function getGenericTypeKeys< ...

Error message received: "ReferenceError: document is not defined when attempting to utilize Angular 8 NgxUsefulSwiper

The NgxUsefulSwiper library is being used in a component and works fine on the local environment. However, upon trying to access the page with the component on the development environment, the following error occurs: ReferenceError: document is not defin ...

What is the best way to determine which DOM element is clicked when using the OnClick event? Can

Upon clicking an element: I seek to pinpoint the exact element. To prevent selecting multiple elements (e.g. with the same class) (and with or without an id), I am considering retrieving the absolute location/path in the DOM. Strategy: I have devised two ...

Is there a way to streamline or simplify these typescript functions?

I am working with two functions: import { handler } from '../lib/handler-lib' import { APIGatewayEvent, Context } from 'aws-lambda' export const producer = handler(async ( _event: APIGatewayEvent, _context: Context ): Promise<ob ...

Directly transfer image to Cloudinary without creating a local file

I have integrated jdenticon to create user avatars upon signup in a node/express app. When working locally, I follow these steps: Create identicon using jdenticon Save the file on my local machine Upload the local file to cloudinary Here is a snippet o ...

Finding the parent form element in jQuery when a specific element is clicked - a simple guide to locating the

After clicking on the Order Now button (<a class="btn btn-lp btn-popup" data-popup-open="popup-1" href="#">Order Now</a>), a popup will appear containing form buttons that are intended to send data to the server. <div class="section-pick-yo ...

Utilizing NPM: downloading various versions of the same library multiple times

Currently, I am heavily involved with Play Framework 2.4 and AngularJs 1.5.8 while also incorporating coffeescript into the mix. As I delve deeper into my project, the use of npm has come under scrutiny. Given that we are utilizing multiple libraries, ea ...

Subscribing and updating simultaneously will not function. Whichever action is attempted, the other will result in an error, and the

import { Component } from '@angular/core'; import { IonicPage, NavController, NavParams } from 'ionic-angular'; import {AngularFireDatabase, AngularFireObject} from 'angularfire2/database'; import{ShoppingItem} ...

Steps to activate a function within an angular6 form-related directive

Is there a way to execute a function within a directive when the form is marked as pristine? I want to apply a CSS class to a tab header when the form is pristine. <form [formGroup]="awayForm" (ngSubmit)="onSubmit()" awayConfirm [cancelClicked]="cancel ...

Is it possible to change individual pixels within a canvas without the need to duplicate the entire buffer?

Is it possible to directly alter individual pixels within a canvas, rather than retrieving the entire buffer using ctx.getImageData and then pasting it back with ctx.putImageData? This is crucial in order to avoid the costly process of copying data every ...

Using optional chaining on the left side in JavaScript is a convenient feature

Can the optional chaining operator be used on the left side of an assignment (=) in JavaScript? const building = {} building?.floor?.apartment?.number = 3; // Is this functionality supported? ...

Refreshing a nested object by modifying its array elements in JavaScript

Hello, I am looking for assistance in updating the userSettings variable. Specifically, when a product is removed from the products array, I need to update the sortedProducts array within the userSettings.categories array. I have attempted to accomplish th ...