When implementing Angular 6, using a shared module within individual lazy-loaded modules can lead to a malfunctioning app when changes are

Hey there, I've encountered a strange problem that didn't occur when I was using Angular 5.

Let me explain the situation:

In my App routing module, I have:

{
    path: 'moduleA',
    pathMatch: 'full',
    loadChildren: './modules/moduleA.module#moduleA'
},
{
    path: 'moduleB',
    pathMatch: 'full',
    loadChildren: './modules/moduleB.module#moduleB'
},

Additionally, I have a Shared module that contains a single component for rating. It is declared and exported as follows:

import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RatingComponent } from './components/rating/rating.component';
import { RatingService } from './services/rating.service';

@NgModule({
    imports: [
        CommonModule
    ],
    declarations: [
        RatingComponent
    ],
    exports: [
        RatingComponent
    ],
    providers: [
        RatingService
    ]
})
export class SharedModule {
    static forRoot(): ModuleWithProviders {
        return {
            ngModule: SharedModule,
            providers: [
                RatingService
            ]
        };
    }
}

The Shared module is imported in both modules. When running `ng serve --aot`, everything works fine. However, if I make changes to the code of Shared, ModuleA, or ModuleB, the CLI compiles successfully but an error occurs in the browser:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'call' of undefined 
TypeError: Cannot read property 'call' of undefined at __webpack_require__ (bootstrap:81)

To resolve this issue, I have to stop the CLI and run it again. This behavior only happens on version 6 (tested with 6.0.7 and 6.1.0). In comparison, there are no issues when using version 5. If I exclude the SharedModule from ModuleA or B, the app works fine, but then the module is not truly shared.

If anyone has insights into what might be causing this issue, your help would be greatly appreciated.

Thank you!

Answer №1

Upon closer examination, it was discovered that the application still functions with a lazyload of modules. However, to prevent encountering the aforementioned issue, it has been determined that all modules previously loaded using lazyload must now be imported in the app.module.ts file.

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

The module 'MatTableModule' could not be located within the '@angular/cdk/table' package during export

Having an issue with using where I'm getting the error message "export 'MatTableModule' was not found in '@angular/cdk/table'. Not sure what I might be doing wrong? You can find more details in my GitHub repository: https://githu ...

The specified type '{ songs: any; }' cannot be assigned to the type 'IntrinsicAttributes' in NEXTJS/Typescript

I am currently working on a straightforward script. Below is the index.tsx file for my Next.js application: import type { NextPage } from 'next' import SongsList from '../components/SongsList/SongsList' import { GetStaticProps } from & ...

Using `await` inside an if block does not change the type of this expression

Within my code, I have an array containing different user names. My goal is to loop through each name, verify if the user exists in the database, and then create the user if necessary. However, my linter keeps flagging a message stating 'await' h ...

Tips for continuously running a loop function until retrieving a value from an API within a cypress project

Need help looping a function to retrieve the value from an API in my Cypress project. The goal is to call the API multiple times until we receive the desired value. let otpValue = ''; const loopFunc = () => { cy.request({ method: &ap ...

Error Handler: Unable to retrieve the error object when utilized with a promise

I'm currently working on an angular application with a custom error handler implementation. One interesting point to note is that when you have a custom error handler and use an observable with http without catching errors using a catch block, like i ...

The inclusion of a custom list preview with prepare in the sanity schema results in errors during the construction

I recently started working with next.js, TypeScript, and Sanity, and everything has been going smoothly so far. I have multiple schemas defined in my project and it works fine in development. The linting checks also do not show any errors. However, when I ...

How can we transfer animation duration and `@keyframe` values through a function in CSS?

In my angular project, I am currently adding animations to text. However, I want to make these animations dynamic. For instance, I have a CSS class called .slide-in-top with an animation time set to 1.3s. Instead of this static value, I want to be able to ...

The functionality to close a Mat dialog is not functioning properly on Angular 11 with Material 11

I am trying to close the dialog by calling the close function of MatDialogRef instance, but unfortunately it is not working as expected. Within my ShareModule, there are components like HeaderCompose and LoginComponent. The HeaderComponent utilizes the Lo ...

"Converting the Request Body into Encoded

One challenge I am facing is the need to encode the body of all outgoing requests in my Angular application, including standard json. While using HttpClient to make requests, it appears that I do not have direct access to the serialization layer within Ang ...

Tips for creating a tailored Express.js request interface using Typescript efficiently

I have been working on designing a custom Express request interface for my API. To achieve this, I created a custom interface named AuthRequest, which extends Request from Express. However, when attempting to import my interface and define req to utilize t ...

IE11 does not support Angular2 Final

After working for a period of time, my implementation suddenly stopped. It seems that I may have imported a component or made some other change causing the issue. Strangely enough, it functions properly on Firefox, Chrome, and Edge, but encounters an error ...

"Discover the power of Next.js by utilizing dynamic routes from a curated list

I am working on a Next.js application that has a specific pages structure. My goal is to add a prefix to all routes, with the condition that the prefix must be either 'A', 'B', or 'C'. If any other prefix is used, it should re ...

Analyzing the object for interface compatibility

When I receive a query string in one of my REST endpoints using koa-router, each value of the query string object parameter is always a string: { count: "120", result: "true", text: "ok" } Within my codebase, I have an Interface that represents the ...

Having trouble using the 'in' operator to search for 'Symbol(StrapiCustomCoreController)' while transitioning Strapi to TypeScript

I'm in the process of converting my strapi project to typescript. I've updated all strapi packages to version 4.15.5 and converted the files to ts extension. However, upon running strapi develop, I encounter the following error: [2024-01-03 10:50 ...

Issue encountered while attempting to execute "npm install --save firebase"

When attempting to run the command "npm install --save firebase", I encountered the error below: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="482e3b2d3e2d263c3b0879667a667c">[email protected]</a> install /U ...

Angular Firebase Count of Items in List

My current dilemma revolves around obtaining a count of items within a firebase list. Firebase details: foods randompushid title: apple, comboQuery: apple23523526(userid) Angular Code snippet: this.foods= this.firebaseDB.list(& ...

Tips for relocating a switcher button using Ant Design Drawer in a React application

*I'm faced with a challenge in my React project where I need to synchronize the movement of a div (switcher-btn) and an Ant Design Drawer. Whenever the Drawer is opened, I want the switcher-btn to move along with it. Below is the code snippet I'v ...

What are the best ways to implement advanced data filtering in JavaScript?

Is there a way to improve the filtering of data in the ngx-datatable? Currently, I have a filter that only works for the "Name" column. How can I adjust it to filter data from other columns like "id" or "phone" as well? filt ...

Typescript: Utilizing Index-Based Callback Parameters in Functions

I am currently working on creating a function that can handle specific data types ("resource") along with an array of arrays containing call objects and corresponding callbacks for when the calls finish ("handlers"). function useHandleResource< R exte ...

removing the mapStateToProps function will result in an undefined value

I am new to React and I'm in the process of converting a class component to functional components using hooks. I need some guidance on safely removing 'mapStateToProps' without encountering undefined errors. I have two pages, A.js and B.js. ...