Ссылка по этой ссылке. Я воспроизвел ваш сценарий в Stackblitz
a.component.html
<button (click)="navTo()">nav to b</button>
a.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'app-a',
templateUrl: './a.component.html',
styleUrls: ['./a.component.css']
})
export class AComponent implements OnInit {
constructor( private router: Router
) { }
ngOnInit() {
}
navTo() {
let item = {
url : `b/${Math.random()}`
};
this.router.navigateByUrl(item.url);
}
}
b.component.html
<h1>Value in param : {{value}}</h1>
<button routerLink="/a">back to a</button>
b.component.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'app-b',
templateUrl: './b.component.html',
styleUrls: ['./b.component.css']
})
export class BComponent implements OnInit {
public value;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.value = this.route.snapshot.params.id;
console.log(this.route.snapshot.params.id);
}
}
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AComponent } from './a/a.component';
import { BComponent } from './b/b.component';
const routes: Routes = [
{
path: '',
component: AComponent
},
{
path: 'a',
component: AComponent
},
{
path: 'b/:id',
component: BComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }