Angular 12 error: Unable to locate route with passed parameters

Explaining how I define the route in my app-routing :

 { path: 'user/:id', component: UserDetailComponent },

Defining the Navigation for Users

<a [routerLink]="['/user', user.id]">detail</a>

Inside the UserDetailComponent

 this.id = +this.route.snapshot.params['id'];

Initially, I can retrieve user details successfully. However, after refreshing the page (user detail/:id) an error message is displayed.

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

Answer №1

For the UserDetailComponent, it is important to set up the NgOnInit method by first defining the following code block:

 const userID = this.route.snapshot.paramMap.get('id');
 this.fetchDetails(userID) //this function is responsible for subscribing to the API call

Answer №2

It seems like you may have overlooked the slash position... In your routing code, there is a slash separating users from the id. You can simply update the code as follows:

<a [routerLink]="['/user', user.id]">detail</a>

Change it to this instead:

<a [routerLink]="['user/', user.id]">detail</a>

Answer №3

It appears that you have specified base="/user" in your index.html, when it should actually be base="/" (assuming you are using a basic npm ng serve command).

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

Ensure that any modifications made to an Angular service are reflected across all components that rely on that service

I am currently in the process of replicating a platform known as Kualitee.com, which serves as a test-management tool utilized by QA Engineers. Within Kualitee, users can access multiple projects, each containing various test cases and team members. The ab ...

Guide on setting the logger level for ngx-logger in the global configuration file

Lately, I integrated ngx-logger into my project to incorporate a logger level within the application. Initially, I hardcoded the logger level in app.module.ts as part of the ngx-logger configuration. However, I now realize the need to move this configurati ...

After encountering an error, the puppeteer promptly shuts down the page

During my page testing, an error is thrown by a dependency. Although the error is not critical and does not impact my application, when testing with Puppeteer and encountering this error, it abruptly closes the tested page. How can I bypass this error to c ...

Alert: The route "/D:/original/22-02-2017/job2.html" specified in [react-router] does not correspond to any existing routes

I am currently working on a project using TypeScript with React. I'm utilizing webpack as a compiler, and my directory structure looks like this: d:/original/22-02-2017/ - In my web.config.js file, the entry point is set to ./src/index.tsx. All ...

Develop a Nativescript Angular component dynamically

Is there a way for me to dynamically generate a Component and retrieve a View object to insert into a StackLayout? ...

Guide on navigating to a different page using a function with router link in Angular using TypeScript

Trying my hand at Angualar and Typescript for the first time. I am working on creating a login page where users can move to another page if their credentials are correct. To achieve this, I want to use a function that is triggered by clicking a button. How ...

Utilizing ng-content repeatedly

I am working on an angular 2 component called my-panel which is designed to show the supplied content using ng-content. However, I am facing an issue where I need to display the same content in an expanded modal as well, but it seems that I can only refere ...

Creating dynamic components from JSON elements does not trigger a rerender of components within an array

Imagine having a simplified input structure like this: [ { type: "text", text: "how are you {name}" }, { type: "input", input: "name" }, { type: "text", text: "good to ...

Even after making changes within my Angular and Firebase subscription, my variable remains unchanged

In an attempt to secure my Angular application's routes, I decided to create a canActivate method that would check the user's status. My backend is based on Firebase, and user authentication, login, and sign up functionalities are all handled thr ...

hiding html elements by using the display property set to none instead of physically removing

I am currently utilizing an if-else statement to display different HTML structures. As a result, the entire HTML is being rendered twice. Is there a way we can utilize 'display: none' instead? I attempted to use it in th ...

What is the best way to create an interface that consistently uses a generic array?

One of my functions has a general structure: export function limitToApiContraints<T extends Array>(payload: T, type: IQueueTypes) { ... } However, there is an issue with the generic signature that prompts the following error message: The gener ...

How do I properly utilize ngIf in Angular2 to display text exclusively when there is no data retrieved from the server?

After retrieving data from the server using ngFor to display it as a search feature, I want to show a message saying 'There's no result' when there are no search results. How can I achieve this? I have attempted the following approach, but ...

What prevents TypeScript from allowing an async function to return a combination of type T or Promise<T>?

During the development of my API in typescript, I encountered a situation where some controller actions can be synchronous while others cannot. To address this issue, I decided to specify a response type as follows: type ActionResult =IHttpActionResult | ...

Update the second select form's list after choosing an option in the first select form in Angular 4

In my form, I have incorporated two select elements within one form tag. The options in the second select element are dependent on the selection made in the first one. Currently, I have set up this functionality using a template-driven approach, with a (c ...

Changing tabs does not automatically refresh the content | Angular

I have implemented a tab system using Bootstrap where each tab contains a form. However, I am facing an issue where the form data from the first tab persists even when I switch to another tab. How can I resolve this? .HTML component: <ul ngbNav #na ...

Issue with detecting window resize event in Angular 7 service

I have a unique service that utilizes a ReplaySubject variable for components, but strangely the WindowResize event isn't triggering. import { Injectable, HostListener } from '@angular/core'; import { ReplaySubject } from 'rxjs'; ...

Encounter Modelstate Issue - ASP.NET Web API authentication - Angular 6 - Making an HTTP Post call

When sending a POST request to ASP.NET Web API Identity - Individual User Authentication Account to register a new user, everything works as expected. However, if the username already exists, an error is returned in the response body's ModelState. I a ...

What causes error TS2345 to appear when defining directives?

Attempting to transition an existing angular application to typescript (version 1.5.3): Shown below is the code snippet: 'use strict'; angular.module('x') .directive('tabsPane', TabsPane) function TabsPane(ite ...

Generate a dynamic key object in Angular/TypeScript

I am working with an object called "config" and an id named "id". My goal is to create an array of objects structured like this: [ "id" : { "config1: ... "config2: ... "config3: ... } "id2" : { "config ...

Angular retrieve - nested mapping

Having trouble with calling an API method inside a map function, as the second API call is not being made without any console errors appearing. //The following method is not getting called - The API method is not being triggered this.getUserPermissions(lo ...