What causes the appearance of the "?" symbol at the beginning of the URL and triggers a reboot of the app when moving through the absolute path?

I am facing an issue.

In my multi-module application with lazy loading, I encountered a strange behavior when trying to navigate between child lazy modules. Transition from top module to bottom child module works fine, but routing from one bottom child lazy module to another causes problems.

Here are some scenarios that work:

=> http://localhost:4200/#/op/services/ =>
=> http://localhost:4200/#/op/services/management/ =>
=> http://localhost:4200/#/op/services/management/edit/123

=> http://localhost:4200/#/op
=> http://localhost:4200/#/op/test/management/
=> http://localhost:4200/#/op/test/management/edit/321

And here is the problematic scenario:

http://localhost:4200/#/op/test/management/edit/321 =>
=> http://localhost:4200/#/op/services/management/edit/123

When I attempt this navigation, the URL starts with '?' and the app undergoes a full reload!

Surprisingly, whether or not a lazy module is loaded does not impact the issue at all.

This problem occurs inconsistently, sometimes the transition works just as intended! Despite attempting various combinations, I have been unable to resolve the issue.

Things I have tried include:

this.router.navigate([op/services/management/edit/${id}]);
this.router.navigate([/op/services/management/edit/${id}]);
this.router.navigate(['/op/services/management/edit/', id]);

this.router.navigateByUrl(op/services/management/edit/${id});
this.router.navigateByUrl(/op/services/management/edit/${id});

routerLink = "/op/services/management/edit/123"
routerLink = "op/services/management/edit/123"

I need help understanding why the route changes unexpectedly.

UPDATE.

Added simplified route configuration:

Base module:

const routes: Routes = [
  {
    path: '', loadChildren: './path/to/op.module#OperatorModule',
    data: {
      preload: false,
      base: true,
    },
  },
  ...
  ];

@NgModule({
  imports: [RouterModule.forRoot(
    routes,
    {
      useHash: true,
      preloadingStrategy: SelectivePreloadingStrategy,
    },
  )],
  exports: [RouterModule],
})
export class AppRoutingModule {}

Operator module containing inner lazy modules:

const routes: Routes = [
  {
    path: 'op',
    component: BasePage,
    canActivate: [AuthGuard],
    children: [
        {
            path: 'services/management',
            loadChildren: './services/services.module#ServicesModule',
            canActivate: [AuthGuard],
            data: {
              preload: true,
            },
        },
        {
            path: 'tests/management',
            loadChildren: './tests/tests.module#TestsModule',
            canActivate: [AuthGuard],
            data: {
              preload: true,
            },
        },
      ...
    ],
  },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class OperatorRoutingModule {
}

Router modules for some of the inner modules:

For services:

const routes: Routes = [
  {
    path: '',
    data: {
        ...
    },
    pathMatch: 'full',
    component: ServicesPage,
    canActivate: [AuthGuard],
  },
  {
    path: 'edit/:id',
    data: {
      accessRole: [UserRole.USER, UserRole.ADMIN],
    },
    component: ServicePage,
    canDeactivate: [CanDeactivateGuard],
    canActivate: [AuthGuard, RoleGuard],
  },
  ....
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class ServicesRoutingModule {
}

For tests:

const routes: Routes = [
  {
    path: '',
    data: {
        ...
    },
    pathMatch: 'full',
    component: TestsPage,
    canActivate: [AuthGuard],
  },
  {
    path: 'edit/:id',
    data: {
      accessRole: [UserRole.USER, UserRole.ADMIN],
    },
    component: TestPage,
    canDeactivate: [CanDeactivateGuard],
    canActivate: [AuthGuard, RoleGuard],
  },
  ....
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class TestsRoutingModule {
}

UPDATE!

I recently discovered a new reason for the problem. The issue arises when attempting to route using an HTML form. I have a CanDeactivate service warning about leaving the page, and even with this service disabled on the page, the window:beforeunload event still triggers on any form submission.

Although it's unclear how these elements are interconnected, I feel like I'm making progress towards solving the issue.

Answer №1

The route button originally had a default type of 'submit', but after changing it to type='button', everything worked perfectly!

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

After submitting a multi-image form from Angular, the "req" variable is not defined

I'm currently facing an issue with submitting a form from Angular 7 to a Node backend using Multer as middleware and Express.json() as bodyParser. While the text data is successfully transmitted to the backend, the image fields are appearing empty {}. ...

Angular Bootstrap Modal not Displaying

<img id="1" data-toggle="modal" data-target="#myModal" data-dismiss="modal" src='assets/barrel.jpg' alt='Text dollar code part one.' /> <div id="myModal" class="modal fade" *ngIf="isModalShowing"> <div class=" modal-lg ...

Creating custom TypeScript validation types at compile time

Is it possible to create custom type definitions in TypeScript that are only checked during compile time? I want users to define a value for a variable (that won't change at runtime) and validate if it meets certain criteria. For example, requiring a ...

Is there a method to categorize an array of objects by a specific key and generate a new array of objects based on the grouping in JavaScript?

Suppose I have an array structured like this. It contains objects with different values but the same date. [ { "date": "2020-12-31T18:30:00.000Z", "value": 450 }, { "date": "20 ...

When emitting an event multiple times in Angular, an error may occur where properties of undefined are unable to be read, particularly in relation to the "

I am encountering an issue with my event binding on a button, specifically (click)="onStart()". The problem arises when the event this.numEmitter is emitted for the first time in setInterval, after which I receive the error message ERROR TypeError: Cannot ...

Enhancing EventTarget in TypeScript (Angular 2+) for ES5 compilation

Is there a way to create a custom class that extends the EventTarget DOM API class? Let's say we have the following class: class MyClass extends EventTarget { constructor() { super(); } private triggerEvent() { this.dispatchE ...

Ag-grid's external filtering feature allows users to easily apply

Initially, I attempted to integrate an external filter, but encountered issues where it wasn't functioning properly, resulting in the grid being stuck in a loading state. As a result, both the filter and grid are currently inaccessible. Currently, I ...

What is the best way to inject a service instance into the implementation of an abstract method?

In my Angular application, I have a service that extends an abstract class and implements an abstract method. @Injectable({ providedIn: 'root', }) export class ClassB extends ClassA { constructor( private service : ExampleService) { s ...

Guidance on transferring information from a parent component to an Angular Material table child component

Currently, I am implementing an angular material table with sorting functionality. You can view the example here: Table Sorting Example I intend to transform this into a reusable component so that in the parent component, all I have to do is pass the colu ...

Checking React props in WebStorm using type definitions

Currently, I am utilizing WebStorm 2018.3.4 and attempting to discover how to conduct type checking on the props of a React component. Specifically, when a prop is designated as a string but is given a number, I would like WebStorm to display an error. To ...

Execute different commands based on operating system using NPM conditional script for Windows and Mac

Scenario: I am currently configuring a prepublishOnly hook in NPM. This hook is designed to remove the "lib" folder, transpile the typescript source files into a new lib folder, and then execute the tests. The issue at hand: There are two individuals re ...

The error message "Declaration file for module 'mime' not found" was issued when trying to pnpm firebase app

Currently, I am in the process of transitioning from yarn to pnpm within my turborepo monorepo setup. However, I have run into an issue while executing lint or build commands: ../../node_modules/.pnpm/@<a href="/cdn-cgi/l/email-protection" class="__cf_e ...

Step-by-step guide on utilizing the vendor.ts file available at https://angular.io/docs/ts/latest/guide/webpack.html

As per the guidelines provided at https://angular.io/docs/ts/latest/guide/webpack.html, it is recommended to include vendors like jQuery in the vendor.ts file. // Other vendors for instance jQuery, Lodash or Bootstrap // You can import js, ts, css, sass, ...

An action in redux-toolkit has detected the presence of a non-serializable value

When I download a file, I store it in the payload of the action in the store as a File type. This file will then undergo verification in the saga. const form = new FormData(); if (privateKey && privateKey instanceof Blob) { const blob = new Blo ...

"Utilizing generic types with the 'extends' keyword to input arguments into a function that requires a more specific

I recently tried out the TypeScript playground and came across a puzzling issue that I can't seem to wrap my head around. Below is the code snippet: type Foo = { t: string; } type Bar = string | { date: Date; list: string; } function te ...

How to identify alterations in user input within Angular?

I need assistance with my search input functionality. I want to ensure that the this.searchProperties.emit is only triggered when the user interacts with the input field by touching it or making an input. The current issue is that the emit function gets ca ...

Issue connecting database with error when combining TypeORM with Next.js

I am attempting to use TypeORM with the next.js framework. Here is my connection setup: const create = () => { // @ts-ignore return createConnection({ ...config }); }; export const getDatabaseConnection = async () => { conso ...

The request.files property in express-fileupload is consistently coming back as undefined

I am trying to achieve the task of uploading a file from my browser and sending it via POST to an Express.js application, which will then download the file using express-fileupload. Here is the client-side JavaScript code I have written so far: // Triggere ...

Does the latest stable version of Angular2 come with a named-routerOutlet feature?

<router-outlet name="another_name"></router-outlet> & the routes are defined as {path: '', component: AnotherComponent} TS is not able to identify this. 'name' is not a valid route parameter Any suggestions on how to ...

Tips for refreshing a React component using incremental changes retrieved from an API

I am developing a unique React application using Next.js and TypeScript, with an api-backed data set in one component that needs to be cached indefinitely. Unlike traditional examples I have found online, my component must: Fetch only the most recent 100 ...