What could be causing routerLink to malfunction despite correct configuration?

Is routerLink properly placed in the view?

<p><a routerLink="/registration" class="nav-link">Register</a></p>

Checking my app.module

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { Routes, RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';

import { AppComponent } from './components/app.component';
import { TestStartComponent } from './components/test-start.component';
import { TestListComponent } from './components/test-list.component';
import { RegistrationComponent } from './components/registration.component';


const appRoutes: Routes = [
    { path: '', component: TestListComponent },
    { path: 'teststart/:id', component: TestStartComponent },
    { path: '**', redirectTo: '/' },
    { path: 'registration', component: RegistrationComponent}
];

@NgModule({
    imports: [BrowserModule, [CommonModule], FormsModule, HttpClientModule, RouterModule.forRoot(appRoutes)],
    declarations: [AppComponent, TestListComponent, TestStartComponent, RegistrationComponent],
    bootstrap: [AppComponent]
})

Details about my RegistrationComponent

import { Component, OnInit } from '@angular/core';
import { UserService } from '../services/user.service';
import { User } from '../models/user';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { first } from 'rxjs/operators';

@Component({
    templateUrl: '../views/registration.component.html',
    providers: [UserService]
})

export class RegistrationComponent implements OnInit {
    constructor(
        private userService: UserService
    ) { }

    ngOnInit() {
        console.log("Initial method called");
    }

Describing registration.component.html

<p>Initial method called</p>

No response when clicking the link. Other routerLinks are functioning correctly.

Answer №1

Requesting an adjustment to the route configuration:

{ path: '', component: TestListComponent }

Ensure 'pathMatch: 'full'' is added to the above route, and relocate the following route to the end:

{ path: '**', redirectTo: '/' }

The revised route setup should look like this:

    { path: '', component: TestListComponent, pathMatch: 'full' },
    { path: 'teststart/:id', component: TestStartComponent },
    { path: 'registration', component: RegistrationComponent},
    { path: '**', redirectTo: '/' }

As per Angular's official documentation:

Using 'pathMatch: 'full'' ensures a complete URL match. Important for avoiding redirect loops with empty-path routes.

Regarding wildcards, according to the docs:

Router matching notation accepts wildcard (**) which matches any URL. Default is "/".

Note that route order matters; the Angular engine processes routes sequentially from top to bottom to find the correct match.

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

How to Disable Back Button During Angular 2 REST API Requests?

Within my Angular 2 application, a REST API call is made to process a functional flow and receive a response from the server. During this process, I want to prevent users from navigating using the browser's Back button. To achieve this, I have implem ...

How to efficiently import an external ES module from a URL using TypeScript

I've recently started experimenting with Observable notebooks and I must say, it's been a great experience so far. Now, my next goal is to integrate a notebook into my web app. The following vanilla JavaScript code using JavaScript modules accomp ...

Angular 8 encountering issues with incomplete/impartial JSON parsing

Upon receiving a JSON object through a Socketio emit, the data appears as follows: { "single":[ { "name":"Xavi555", "data":[ { "_id":"5e2ea609f8c83e5435ebb6e5", "id":"test1", "author":"someone", ...

How to retrieve a Typescript variable within an ngIf conditional statement in Angular

How can I access a TypeScript variable inside an ngIf statement? TS: public data: string; HTML: <td> <div *ngIf="data === 'BALL' ; else noplay" >{{ play }}</div> <ng-template #noplay> {{ gotohome ...

Unleash the power of zod by seamlessly accessing both parameters and queries

In the zod-middleware documentation, an example is provided: export async function endpointCode(req: TypedRequestBody<typeof bodySchema>, res: Response) { const typedBody = req.body; return res.json(typedBody); } This example demonstrates access ...

"Enhancing the functionality of @angular/fire by transitioning from version 6.x to 7.x

I need to update my app dependencies and code from @angular/fire 6.x to 7.1.0-rc4 in order to access a feature that is not available in the 7.0.x version. I made the necessary changes in my package.json as follows: "@angular/fire": "~7.1.0-r ...

The element 'mat-slide-toggle' is unrecognized following the import

I'm currently facing an issue with using <mat-slide-toggle>Click me!</mat-slide-toggle> in a component that already has the MatSlideToggleModule imported. Strangely, I'm still encountering an error stating that it's not a recogni ...

What is the best way to display a popup message confirming successful login validation?

I want to implement a login success popup that only appears after validation and user verification. Currently, the popup is displaying before validation upon clicking the submit button. I want the popup to show only after successful validation and user v ...

Leverage the Angular2 component property when initializing a jQuery function

I'm currently developing a web app with Angular 2 and utilizing jQuery autocomplete. When making requests to the remote server for completion data, I found that the server address is hardcoded in the autocomplete function. Even though I tried using co ...

Why include HTML in our webpack bundle?

Hello there, I am currently working on an angular 2 app using this starter pack as a base. I am trying to understand what our build process will entail. I have noticed that when running: npm run build:prod npm run server:prod The HTML content is incl ...

The e2e directory seems to be missing from my Angular project

https://i.stack.imgur.com/xzrlb.png Despite reinstalling the CLI, the E2E feature still eludes me. ...

Stop users from logging in simultaneously on multiple systems

It is possible for the same user and password to be used on multiple computers simultaneously! If person 1 is logged in with a certain username and person 2 logs in from another computer or browser using the same credentials, person 1 will not be automatic ...

Angular's capability for manipulating data asynchronously

I am relatively new to this, and I am facing difficulties in handling data manipulation in the frontend of an app that I'm currently developing. In my code, there are two functions named "getTipoEtapas()" and "getEtapasporTransfo" which utilize two s ...

Ways to access the values of checkboxes that are initially checked by default

Recently, I was working on a project that involved multiple checkboxes. My goal was to have the checkboxes pre-checked with values in the form (using reactive form). Users should be able to unselect the boxes as they wish and the data would be stored accor ...

Trouble displaying JSON markers on Ionic Google Maps

Looking for assistance related to this issue? Check out Ionic Google Maps Markers from JSON In my Ionic App with Google Maps integration, I'm facing an issue where although the map displays correctly, the pin markers are not showing up. It seems like ...

Ways to incorporate fresh features through button clicks

Within a table cell, I am using ngClass with a condition like this: <tr> <td [ngClass]="{active: !show, inactive: show}"></td> <td></td> </tr> This ngClass is toggled on button click by the following function: ...

Issue with starting @mauron85/cordova-plugin-background-geolocation on Ionic 5 and Angular 9 platform

I'm facing a challenge with integrating the background geolocation plugin into my app. Here is the documentation link for reference: https://ionicframework.com/docs/native/background-geolocation Here's the snippet of my code that initiates the p ...

Error message: "Cannot access property 'push' of null in Angular FormArray"

Working with Angular Reactive FormArray to dynamically add multiple inputs upon clicking the "Add" button. Encountering an error message stating "Cannot read property 'push' of null" when attempting to add/push input fields. Considering if ther ...

Setting Angular FormControl value to null within a service

My Angular form is reactive and collects mobile numbers along with other details. Here is the code snippet: component.html <form [formGroup]="contactDetailsForm"> <ngx-intl-tel-input [cssClass]="'ngxIntlInputBorder'&quo ...

specialized registration process with auth0 in Angular

I am attempting to enhance the user information in a single call. The process involves first signing up with a username and password on Auth0, followed by adding additional userinfo to the database during the callback phase. However, I am encountering diff ...