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.