Решением было бы собрать параметр маршрута из URL.
{ path: '/users/:username', component: 'userDetailedComponent' }
Исходя из вышесказанного, вы передаете имя пользователя вместе с маршрутом. Теперь в ngOnInit вы можете собрать имя пользователя из URL-адреса с помощью кода ниже
import { ActivatedRoute } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { Service } from './service.service';
@Component({
selector: 'app-userdetails',
templateUrl: './userdetails.component.html',
styleUrls: ['./userdetails.component.css']
})
export class UserdetailsComponent implements OnInit {
constructor( private service: Service, private router1: ActivatedRoute) { }
username:string ='';
ngOnInit(): void {
this.username = this.router1.snapshot.paramMap.get('username'));
this.service.getdetails('username').subscribe((data)=>{
console.log(data);
});
}
. В приведенном выше коде ongOnInit мы объявили переменную, считывающую параметр url, используя ActivatedRoutes .
this.router1.snapshot.paramMap.get ('username')) извлечет имя пользователя из URL. Это имя пользователя может быть отправлено в службу, и могут быть получены необходимые данные.
Надеюсь, это поможет.