I have the following routes which should route to the same component, I can get it to work by using this following structure.
const carModuleRoutes: Routes = [
{ path: 'car/:category/:carId', component: CarDetailComponent},
{ path: 'car/:carId', component: CarDetailComponent},
];
But I know this is not a standard way to do so, Is there a proper way of doing the same functionality ?
You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios. Any component that gets matched by the Router will render it as a sibling of the Router outlet.
Setting up wildcard routeslinkThe Angular router selects this route any time the requested URL doesn't match any router paths. To set up a wildcard route, add the following code to your routes definition. The two asterisks, ** , indicate to Angular that this routes definition is a wildcard route.
What is the difference between [routerLink] and routerLink ? How should you use each one? They're the same directive. You use the first one to pass a dynamic value, and the second one to pass a static path as a string.
Dynamic Routing means, that the route configuration of a module can vary at each load depending on the logic you define for the loading. The main trick is to use the ROUTES InjectionToken which is a low-level API for the router configuration.
How to load different component under same route in Angular. For example, if user lands on home page and he is not logged in, we want to show him landing page, but if he is logged in for same url we want to show him logged in section page, for example lates news page. This can be done in Angular routing module.
Angular Routing. Working with same routes, sub-routes… | by Jide Lambo. | codeburst Jide Lambo. Working with same routes, sub-routes and redirecting routes. One of the best attributes of using Angular is the ability to create single page application (SPA). And what better way to have a SPA than to have your pages route magically.
When a route gets activated for a component, it displays the view of that component. In your 'app.modules.ts', import RouterModule from '@angular/router' . This will register the Angular router provider. The forRoot ( []) method signifies that the routes are available to your root application module.
The first approach is to tell the Angular router to emit the route events on refresh and then handle them accordingly in our routed component. In early Angular versions, there was no option to tell the router to emit events on same route refresh. Angular 5.1 introduced the onSameUrlNavigation property on the routers ExtraOptions.
That is the standard way of defining routes
and their respective components
. You are inside routing
module and you are trying to define routes
, and not the components
.
So, do not overlook to group the same component
and define routes
for them. What you have done seems appropriate.
EDIT :
Your case is very simple, you can simply do :
const carModuleRoutes: Routes = [
{ path: 'car/:category/:carId', redirectTo : 'car/:carId', pathMatch : 'full'},
{ path: 'car/:carId', component: CarDetailComponent},
];
Note :
In this case, your url will change and you might loose the value of :category
, which might have been useful in the component. So it is best to specify multiple routes pointing to same component as a seperate case.
its ok to use also you can use redirect and child routing for same component. You have done it right way .
Refrence:https://angular-2-training-book.rangle.io/handout/routing/routeparams.html
Child Routing REF : https://angular.io/guide/router#child-routing-component
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With