Попробуйте:
Настройка маршрутизатора:
{
path: 'main/:id',
component: MainComponent
},
В вашем основном компоненте:
ngOnInit() {
this.route.params.subscribe(params => {
const id = params['id'];
this.loadData(id);
});
}
Рабочий пример Stackblitz .
ОБНОВЛЕНИЕ
Чтобы передать полученное значение, попробуйте следующее:
main.component.html (передайте полученный идентификатор в свойство "id" компонента контакта @Input)
<app-contact [id]="id"></app-contact>
contact.component.ts
import { Component, OnChanges, SimpleChanges, Input } from "@angular/core";
@Component({
selector: "app-contact",
templateUrl: "./contact.component.html",
styleUrls: ["./contact.component.css"]
})
export class ContactComponent implements OnChanges {
@Input() id;
constructor() {}
ngOnChanges(changes: SimpleChanges): void {
if (changes["id"]) {
console.log(`call backend to get data for [${this.id}]`);
}
}
}
new Stackblitz .