如何允许路由获取url localhost:4200 / overview? Partid =数字

问题描述 投票:0回答:1

问题

如何更改路由以获取此URL本地主机:4200 /概述? Partid =数字?

这里将任何动态数字都编号为5等。

在我的角度应用程序的URL上写时我需要

localhost:4200 /概述? Partid =数字?

然后它将转到概述组件,并且它将在URL中更改为

localhost:4200 /概述? Partid =数字?

所以如何更改路由以获取此地址,这是指定的URL

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 
  }
angular typescript angular7 angular7-router
1个回答
0
投票

在app-routing.module.ts中

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

在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']);
});
© www.soinside.com 2019 - 2024. All rights reserved.