Error in Angular 7: ActivatedRoute paramId returns null value

On page load, I am trying to subscribe to my paramsID, but when I use console.log(), it returns null. I am currently working with Angular 7.

Here is my TypeScript code:

    import { Component, OnInit } from '@angular/core';
    import { ActivatedRoute } from "@angular/router";
    @Component({
     selector: 'c:navbar',
     templateUrl: './navbar.component.html',
     styleUrls: ['./navbar.component.scss']
    })
    export class NavbarComponent implements OnInit {
     selectedItem: string;
     selectedFlag: string;
     paramsId: any;
    constructor(private _ActivateRoute: ActivatedRoute) { }

    ngOnInit() {
    this._ActivateRoute.paramMap.subscribe(params => {
    this.paramsId = params.get("activeLang");
    console.log('Get Param',this.paramsId);
    });
   }

This is my route setup:

   import { NgModule } from '@angular/core';
   import { Routes, RouterModule } from '@angular/router';
   import { HomePagesComponent } from './pages/home-pages/home- 
    pages.component';
   import { PersonalPagesComponent } from './pages/personal- 
    pages/personal-pages.component';


   const routes: Routes = [
  {path: '', redirectTo: 'en/home', pathMatch: 'full'},
  {path: ':activeLang/home', component: HomePagesComponent},
  {path: ':activeLang/personal', component: PersonalPagesComponent},
  {path: ':activeLang/business', component: BusinessPagesComponent},
   ];

   @NgModule({
   imports: [RouterModule.forRoot(routes, {scrollPositionRestoration: 
             'enabled'})],
   exports: [RouterModule]
   })
   export class AppRoutingModule { }

I am unsure if my code is correct because null is being returned when I use console.log(). Can someone help me troubleshoot this issue?

The expected result should be the output of "en" in the console.

Answer №1

Here is a suggestion to consider:

Assign the value of this._ActivateRoute.snapshot.paramMap.get("activeLang") to this.paramsId.
Then, log this.paramsId to the console.

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

Expo + tRPC: Oops! Looks like the application context couldn't be retrieved. Don't forget to wrap your App inside the `withTRPC` HoC for

I'm currently working on a straightforward tRPC server setup: // server.ts import { initTRPC } from "@trpc/server"; import { z } from "zod"; const t = initTRPC.create(); export const appRouter = t.router({ greeting: t.procedu ...

Guide to incorporating a pluck feature into TypeScript code

One task I face frequently is extracting specific properties from an object const obj = {a:1, b: 2, c: 3}; const plucked = pluck(obj, 'a', 'b'); // {a: 1, b:2} Unfortunately, achieving this task with type safety in TypeScript can be c ...

When utilizing destructuring in React.js with TypeScript, incorrect prop values are not being alerted as expected

I recently started using TypeScript and I have been enjoying it so far. However, I came across an issue today that I couldn't solve. Imagine a scenario where a parent component A passes a function that expects a numeric value to the child component B ...

Tips for populating class attributes from an Angular model

Suppose there is a Class Vehicle with the following properties: public id: number; public modelId: number; public modelName: string; Now consider we have an object that looks like this {id: 1, modelId: 1, modelName: "4"} What is the best way to assign e ...

Node.js is known for automatically adding double quotes to strings. However, is there a way to eliminate them?

After creating a unit test, I noticed that both logics I used led to the same issue. Logic 1: describe('aresFileCopier', () => { test('log error', async () => { await registerDB('ares-test', { client: ' ...

Creating an RxJS observable stream from an event emitted by a child element in an Angular 2 component template

Currently incorporating Angular 2.0.0-rc.4 alongside RxJS 5.0.0-beta.6. In the midst of exploring various methods for generating observable streams from events, I find myself inundated with choices and would like to gather opinions. Recognizing that there ...

Numerous instances of Duplicate Identifier errors were encountered during testing of the TypeScript application

After spending all morning searching for a solution... I am using TypeScript with node, npm, bower, and gulp. Whenever I run gulp serve or gulp test, I keep getting the same error message hundreds of times: src\app\main\common\dialogs ...

What is the reason for the inability of `Array<Value>, Value` to properly retrieve the type of the array value?

I am encountering an issue with the type declaration below: function eachr<Subject extends Array<Value>, Value>( subject: Subject, callback: ( this: Subject, value: Value, key: number, subject: Subject ...

"Ionic Calendar 2 - The ultimate tool for organizing your

I am using a calendar in my Ionic app that retrieves events from a database through an API. var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://portalemme2.com.br/SaoJoseAPI/agenda', true); this.http.get('http://portalemme2.c ...

The router.navigate() function seems to be malfunctioning as it is not working as

I have a method defined as follows: private redirect(path: string): void { this.router.navigate([path]); } This method is called within another method like so: private onError(error: any): void { switch (error.status) { case 401: / ...

React.js: You cannot call this expression. The type 'never' does not have any call signatures

Could someone help me troubleshoot the error I'm encountering with useStyles? It seems to be related to Typescript. Here's the line causing the issue: const classes = useStyles(); import React from "react"; import { makeStyles } from & ...

Tips for triggering an event from a function instead of the window

Everything is functioning as expected, with the event listener successfully capturing the custom event when it is dispatched from the window and listened for as loading, all seems to be working well. const MyLib = mylib(); function mylib() { const re ...

What sets apart .to and .toService in InversifyJS?

I find the documentation on using .toService(MyClass) for transitive bindings confusing. The examples provided also show that achieving the same result is possible with a regular .to(MyClass). https://github.com/inversify/InversifyJS/blob/master/wiki/tran ...

Refreshing functional component upon change in property of different class [MVVM]

I've been tasked with incorporating MVVM into React for a class assignment. To achieve this, I've opted to use functional components for the view and TypeScript classes for the ViewModel. However, despite successfully updating properties in the V ...

The display of the selected input is not appearing when the map function is utilized

I am attempting to use Material UI Select, but it is not functioning as expected. When I use the map function, the default value is not displayed as I would like it to be. However, it does work properly when using the traditional method. *** The Method th ...

What steps can be taken to resolve the TS5023 error that arises when including "allowImportingTsExtensions" in the tsconfig.json file?

While working on my React project, I've encountered a specific error that reads: Could not parse tsconfig.json. Please ensure it contains valid JSON syntax. Details: error TS5023: Unknown compiler option 'allowImportingTsExtensions'. I tr ...

Having trouble getting undefined values for keys while attempting to retrieve all the data from Firebase DB with Angular

Currently, I have code that is fetching records from the Firebase database using both Angular and Ionic. The code functions properly, but it does not provide me with the keys for each record. Instead, it returns 'undefined'. I have researched s ...

Modify the value of mat-slide-toggle from TypeScript code

This is the HTML code I have for a mat-slide-toggle element, with a toggleStatus() function: <span class="form-control form-control-plaintext"> <mat-slide-toggle name="status" checked="" ...

Utilizing Vue 3 props validation in conjunction with the power of Typescript

Looking to customize a Link component using Nuxt, Typescript, and the composition-api. The prop target can accept specific values as outlined below. I'm curious if using a custom validator function to check prop types at runtime adds value when compar ...

The function was operational before, but currently it is indicating that it is no longer functioning as a

I encountered an issue with my code where a service that was previously working has suddenly stopped functioning and is now displaying an error message stating that it's not a function. Thanks to Sajeetharan for helping me out initially. constructo ...