Active Angular component utilizing *ngIf during the programmatically lazy loading of a Module

I find myself in a situation where I need to load numerous components on a specific route within my application. These components are controlled by a logic with *ngIf directives that allow me to show or hide them dynamically.

For instance:

<div *ngIf="active_slot_one"> <component-one></component-one> </div>
<div *ngIf="active_slot_two"> <component-two></component-two> </div>
<div *ngIf="active_slot_three"> <component-three></component-three> </div>
<div *ngIf="active_slot_four"> <component-four></component-four> </div>

These components are imported from different libraries that I have created. Therefore, in the module of the parent page, I must import all modules that declare and export these components from my libraries.
I want to import this module lazily, so I attempt to import the required module only when the corresponding variable active_slot_x becomes true using the following function:

loadModule(value: boolean, module: string) {
    switch(module) {
        case 'one': 
            !!value && (await import('lib-one').then(m => m.LibOneModule));  // line for lazy loading module
            this.active_slot_one = value;
        case other_case:
            // same as above
    }
}

By utilizing this function, I activate and deactivate the correct components. By removing all imports from my parent page module and inserting the line mentioned above before activating the ngIf, I anticipate the components will load correctly.
Upon compiling my app, the main.js file is smaller than before and Module One appears in the Lazy Chunk Files.

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

Furthermore, the modules are imported precisely when needed.

https://i.sstatic.net/3BqOP.png

However, upon activating the ngIf, the component does not display on the page.

What could be the issue here?

----- ADDITIONAL INFORMATION -----
When statically importing Module One into the parent page module, activating the ngIf shows the slot with the component inside, along with the indicator arrow signaling content within the component:

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

Conversely, with lazy loading implemented using the aforementioned function, the DOM consistently displays the slot with the component inside, but the component itself appears empty:

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

Answer №1

Could you demonstrate where the "loadModule" function is utilized?

From my understanding, a provider needs to be established in the component where you want to use it.

Alternatively, you could inject the components at the appropriate position within the component after its initialization.

In simpler terms, it appears that there may be some confusion regarding the life cycle hook.

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

Mapping the response from an http.get call to create a new typed object instance in Angular 2

Can anyone help me understand how to map the result from a service call to an object using http.get and Observables in Angular 2? Please check out this example In my getPersonWithGetProperty method, I am trying to return an Observable of type PersonWithG ...

Tips for implementing ID enforcement on downgraded Angular 2+ component

Currently in the process of transitioning AngularJS 1.6 components to Angular 4.3.2, I encountered a need to implement an id attribute on the component tag. However, in order to integrate ng2+ with existing ng1 components, downgrading is required: angular ...

Steps for linking HTTP requests in Angular 2 depending on the type of response

My attempt to create an api call from a remote server and then, if an error occurs, make another request from my local server is not working as expected. I am encountering errors and need help to determine if my approach is feasible. Here is the code snip ...

An easy way to insert a horizontal line between your text

Currently, I have two text responses from my backend and I'm considering how to format them as shown in the design below. Is it possible to automatically add a horizontal line to separate the texts if there are two or more broadcasts instead of displa ...

Router failure resulted in an internal server error

When navigating to a page in my router, I make a REST API request to retrieve data from the server in the beforeEnter clause as shown below: beforeEnter: (to, form, next) => { getData().then( (response) => { ...

When utilizing WPF WebBrowser with a local Angular 8 website, the window.external feature works flawlessly. However, complications arise when

Apologies for any formatting issues, this is my first post. I have been working on updating a legacy app with a new UI. This serves as a proof of concept to demonstrate our ability to create a thin client and host our project online. In my WPF window (.NE ...

Using Typescript and react-redux with a Stateful Component: The parameter type 'typeof MyClass' does not match the expected component type

I've been trying to dive into the world of Typescript, React, and Redux. However, I've hit a roadblock at the moment. This is the error I encountered: ./src/containers/Hello.tsx [tsl] ERROR in /home/marc/Development/TypeScript-React-Starte ...

Angular2 RC5 causing switchMap to fail in ngrx/effects

Recently in my Angular2 application, I've been utilizing ngrx/effects. However, with the release of RC5, I started encountering errors in my code. Here's a snippet: import { Injectable } from '@angular/core'; import { Effect, Actions } ...

What is the method for utilizing an alias at the top within a subquery?

Is it possible to use an alias from the top query in a subquery? I am currently encountering an error with 'artc'. let myQuery = this.articles.createQueryBuilder('artc') .select(['artc.title']) .addSelect(qb => ...

Angular: You cannot assign a type of 'void' to a parameter that expects either a UserCredential or a Promise containing a UserCredential

Upon attempting to initiate a new method following a successful registration, I encountered an error in Webstorm: The argument type 'void' cannot be assigned to the parameter type '(value: UserCredential) => UserCredential | PromiseLik ...

Looking for giphy link within a v-for loop (Vue.js)

I am fetching a list of movie characters from my backend using axios and rendering them in Bootstrap cards. My objective is to search for the character's name on Giphy and use the obtained URL as the image source for each card. However, when I attemp ...

Utilizing Nodemailer and ReadableStreams to send email attachments stored in S3

My current challenge involves sending emails with Nodemailer that include attachments hosted in S3, utilizing JS AWS SDK v3. The example provided in the Nodemailer documentation demonstrates how to send an attachment using a read stream created from a file ...

Error TS2322: The function expecting a type of 'FormEventHandler<HTMLFormElement>' cannot be assigned the type '(data: TicketFullDTO) => Promise<void>'

I am currently working on creating an edit form to modify data from a database based on its ID. Here is my approach: import React, {FormEvent, useEffect, useState} from "react"; import TextField from "@material-ui/core/TextField" ...

Encountering a problem when trying to use event.target.value in an Angular TypeScript application

Here is the code from my app.component.html : <h1>Password Generator</h1> <div> <label>Length</label> </div> <input (input)="onChangeLength($event.target.value)"/> <div> <div> <input ...

What is the best way to create a method that waits for a POST request to complete?

I have the following code snippet: login() { const body = JSON.stringify({ "usuario":"juanma", "password":"1234"}); console.log(body); let tokencito:string = '' const params = ne ...

What is the best way to create a React component that renders a class component as a functional component?

My Objective: At the moment, I am in the process of developing an AuthUserRole HOC component to manage user roles like Manager and Employee. However, I encountered a tutorial that uses a functional component to return a class component as referenced here. ...

Can we find a superior method?

I am currently using a stateful service to retrieve questions. The service checks if the questions exist in the local state, and if they do, it returns them. Otherwise, it makes an HTTP call to the API to fetch and store the questions. The service method ...

Manipulating Typescript JSON: Appending child attributes and showcasing them alongside the parent item attributes

Here is a JSON data that needs to be processed: var oldArr = [{ "careerLevel": "Associate", "careerLevels": [ { "201609": 21, "201610": 22, "careerID": "10000120" }, { "201609": 31, "201610": 32, ...

Enhancing TypeScript builtin objects in Netbeans using a custom plugin

While in the process of converting JavaScript code to TypeScript, I encountered a challenge with extending built-in objects using Object.defineProperty, such as String.prototype. Object.defineProperty(String.prototype, 'testFunc', { value: funct ...

Deleting an element from a two-field object in TypeScript 2

I am working with a 2-field object structure that looks like this { id: number, name: string } My goal is to remove the name field from this object. How can I achieve this in TypeScript? I have attempted using methods like filter and delete, but all I r ...