When using Ionic 4 with Angular, a useful solution involves handling different paths, specifically with optional ids in the URL:
{ path: 'landing', loadChildren: () =>
import("./landing/landing.module").then(m => m.LandingPageModule)},
{ path: 'landing/:id1', loadChildren: () =>
import("./landing/landing.module").then(m => m.LandingPageModule)},
{ path: 'landing/:id1/:id2', loadChildren: () =>
import("./landing/landing.module").then(m => m.LandingPageModule)}
The target remains constant throughout. The component is where the magic truly starts:
import { ActivatedRoute, Router } from "@angular/router";
@Component({
selector: "landing",
templateUrl: "./landing.page.html",
styleUrls: ["./landing.page.scss"]
})
export class landingPage implements OnInit {
constructor(
private route: ActivatedRoute,
private router: Router
) {}
ngOnInit() {
let id1= this.route.snapshot.paramMap.get("id1");
let id2= this.route.snapshot.paramMap.get("id2");
// Testing
if (id1) console.log(id1);
if (id2) console.log(id2);
}
}
Transitioning from page to page can be done as follows:
this.router.navigateByUrl('/landing');
this.router.navigateByUrl('/landing/' + '123');
this.router.navigateByUrl('/landing/' + '123/333');
The sequence in which these are executed matters, and remember to retain the trailing slash on the initial path.