Refreshing an Angular page when navigating to a child route

I implemented lazy loading of modules in the following way:

{
path: 'main',
data: {title: ' - '},
component: LandingComponent,
resolve: { images: RouteResolverService },
children: [
  {
    path: '', redirectTo: 'home', pathMatch: 'full',
  },
  {
    path: 'home',
    loadChildren: () => import('../app/home/home.module').then(mod => mod.HomeModule),
  },
  {
    path: 'courses',
    loadChildren: () => import('../app/courses/courses.module').then(mod => mod.CoursesModule),
  },
  {
    path: 'students',
    loadChildren: () => import('../app/students/students.module').then(mod => mod.StudentsModule),
  },
  {
    path: 'forum',
    loadChildren: () => import('../app/forum/forum.module').then(mod => mod.ForumModule),
  },
  {
    path: 'contact',
    loadChildren: () => import('../app/contact/contact.module').then(mod => mod.ContactModule),
  },
  {
    path: 'profile',
    loadChildren: () => import('../app/profile/profile.module').then(mod => mod.ProfileModule),
  }
]

}

If I am on the route 'main/profile' and refresh the page, it automatically navigates to 'main/home'. I understand why this is happening, but I need assistance in preventing this behavior.

My goal is that when I am on 'main/profile' and refresh the page, I should remain on 'main/profile'.

Answer №1

Give this a shot:

constructor(public activatedRoute: ActivatedRoute, private routerService: Router) {
    this.loading = true;
    this.parameter1 = this.activatedRoute.snapshot.params.param1;
    this.parameter2 = this.activatedRoute.snapshot.params.param2;
    // fetch data
}

goToSubPage(subParam: string) {
    this.routerService.navigate([subParam], { relativeTo: this.activatedRoute });
}

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

Angular Express redirect and how to implement it

My SSR response service includes a redirect method along with other functionalities: import { Injectable, Inject } from '@angular/core'; import { Response } from 'express'; import { RESPONSE } from '@nguniversal/express-engine&ap ...

Exploring Typescript keyof in Storybook's User Interface customizations

Currently, I am working on developing components for integration with Storybook, but I am encountering an issue related to Typescript inferred types. While striving for code reusability, I prefer not to specify the options for a control within the story i ...

Modified the imports within Angular Material

I recently began developing a new website using Angular 9 and Angular Material. Initially, this code snippet worked fine: import {MatSidenavModule, MatToolbarModule} from '@angular/material'; However, I encountered an error that prompted me to u ...

"Excel file becomes inaccessible after being downloaded from server using Lambda function with Node.js and Angular

My Nodejs code generates an excel file, which is then transformed based on client request. I am sending a request from an Angular frontend. The code utilizes exceljs to create the excel file. let [row] = await conn.execute( "Query& ...

The service value remains unchanged following a click event

I'm using a service to share values between 2 components. I am displaying this value in one component and trying to change it in the other by clicking on a button. However, the value is not being modified as expected. It seems like I may have some tro ...

Ways to link a Database with an Angular Web App

Hello all, I am embarking on a project where I need to showcase and modify data from an existing PostgreSQL database within an Angular Web Application. As a complete newcomer to Angular and related technologies, I have taken the initial steps by downloadin ...

Tips for accurately defining the return type for querySelector(All) connections

I enjoy doing this particular task, ensuring the types are correct. const qs = document.querySelector.bind(document) as HTMLElementTagNameMap | null; const qsa = document.querySelectorAll.bind(document) as NodeListOf<any>; While hovering over query ...

The disappearance of the "Event" Twitter Widget in the HTML inspector occurs when customized styles are applied

Currently, I am customizing the default Twitter widget that can be embedded on a website. While successfully injecting styles and making it work perfectly, I recently discovered that after injecting my styles, clicking on a Tweet no longer opens it in a ne ...

Color changes on mat-calendar when hovering

Is it possible to change the hover color of the Mat-calender element? I managed to do so using this CSS code: .mat-calendar-body-cell-content:hover { background-color:#something } The issue is that when hovering the cursor in the corner of the cell, the ...

Bidirectional data binding in Angular 2 allows for communication between parent components and directives

Update: Experimenting with Angular2 Beta, I am working on incorporating an "editor" component template that includes a directive wrapping the Ace editor. In this scenario, the "editor" component acts as the parent of the Ace wrapper directive, and my goal ...

Invoking a controller from another controller in the Express framework using Typescript

Currently, I am trying to call a controller from another controller in my code. While attempting to pass parameters using {params: {task_id: String(task_id), result}}, TypeScript is flagging an error indicating that res does not have all the required attri ...

Steps to include a Target property in a freshly created MouseEvent

Trying to dispatch a contextMenu event, I've noticed that in the MouseEvent interface for TypeScript, the target property is missing, even though it is documented in the contextMenu documentation. Here's my TypeScript snippet: const emulatedMou ...

The attribute 'status' is not found in the 'ServerResponse' type (TS2339)

I've attempted to develop an interface and install React types, but the only way it seems to work is when I write the code in JavaScript. However, within a TypeScript project, I encounter this error: Property 'status' does not exist on typ ...

One way to display a table is by populating it with data from an API. If the table does

Within my Angular 6 application, there exists a table that displays data fetched from a web api. Additionally, I have incorporated some ngIf containers. One of these containers is programmed to exhibit a message in case the web api data turns out to be emp ...

Error Encountered: 403 Forbidden Error when Loading PDF in Quill Project using Uploadthing URL

This project is a combination of Next.js with TypeScript, tRPC, MySQL using Prisma, and Pinecone for vector DB. Uploadthing is integrated for PDF uploads. Despite successful upload, there is an issue when trying to open the PDF on the website, resulting in ...

Having trouble entering text into a React input field

Encountering a puzzling issue with a simple form featuring an input field that inexplicably won't respond to keyboard typing. Initially, suspicions pointed towards potential conflicts with the onChange or value props causing the input to be read-only. ...

I suggest installing the ng2-search-filter package for easy search functionality

Is there a recommended alternative to ng2-search-filter? I encountered an error with this one: 'Ng2SearchPipeModule' does not appear to be an NgModule class.(-996002). The error message in ng2-filter.module.d.ts(1, 22) suggests that the library ( ...

Display streaming data continuously within an HTML page using Angular 16

Currently, I am actively developing a stream API that receives data with a 'Content-Type' of 'text/event-stream'. Below is a snippet from my stream.service.ts: connectToSse(): Observable<any> { return new Observable((observer ...

Utilizing a personalized component with a mat-autocomplete feature within reactive forms by nesting

I'm struggling to add a nested component to the parent form group when the component only contains a mat-autocomplete. I attempted the same method used for the address subform which worked well, but for the user search, it's required to pass in t ...

Encountered a hitch while trying to start an angular 2 app using npm

Encountering an error while compiling the application with @angular/cli version 1.2.4 @angular/cli: 1.2.4 node: 8.11.1 os: win32 x64 @angular/animations: 4.3.6 @angular/common: 4.3.6 @angular/compiler: 4.3.6 @an ...