How to allow routing to get url localhost:4200/overview? Partid=number

1.1k Views Asked by At

problem

How to change routing to get this URL localhost:4200/overview? Partid=number ?

number here any dynamic number as 5 etc .

I need when write on URL of my angular app

localhost:4200/overview? Partid=number ?

then it will go to overview component and it will change in URL as

localhost:4200/overview? Partid=number ?

so How to change routing to get this is URL specified

app-routing.module.ts

import { QualificationsComponent } from './Pages/qualifications/qualifications.component';
import { FamilyComponent } from './Pages/family/family.component';
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { OverviewComponent } from './Pages/overview/overview.component';
import { ManufacturingComponent } from './Pages/manufacturing/manufacturing.component';
import { PackageComponent } from './Pages/package/package.component';
import { ParametricComponent } from './Pages/parametric/parametric.component';

const routes: Routes = [
  { path: '', component: OverviewComponent },
  { path: 'overview', component: OverviewComponent },
  { path: 'family', component: FamilyComponent },
  {path:'manufacturing',component:ManufacturingComponent},
  {path:'package',component:PackageComponent},
  {path:'parametric',component:ParametricComponent},
  {path:'qualifications',component:QualificationsComponent},
];

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

overview component

import { CompanyService } from './../../service/company.service';
import { Component, OnInit } from '@angular/core';
import { PartDetailsService } from 'src/app/service/part-details.service';



@Component({
  selector: 'app-overview',
  templateUrl: './overview.component.html',
  styleUrls: ['./overview.component.css']
})
export class OverviewComponent implements OnInit {


  public companyProfile;
  constructor(public partDetailsService: PartDetailsService
    ,         public companyService: CompanyService) {

   }

  ngOnInit() {
     //How to catch or rcognize Partid=10 here on component overview 
  }
1

There are 1 best solutions below

2
On BEST ANSWER

In app-routing.module.ts

{ path: 'overview/:Partid', component: OverviewComponent },

In Component.ts

import { Router, ActivatedRoute, ParamMap } from "@angular/router";
////
constructor(public partDetailsService: PartDetailsService
    ,         public companyService: CompanyService, public route: ActivatedRoute)
////
this.route.params.subscribe((params: ParamMap) => {
                const Partid = Convert.StringToInt(params['Partid']);
});