Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 - Routing to the same component with different URL's

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 ?

like image 869
Am Novice Avatar asked Jun 20 '18 09:06

Am Novice


People also ask

Can we use 2 router outlet in Angular?

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.

What is wildcard routing in Angular?

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 difference between routerLink and routerLink?

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.

What is dynamic routing in Angular?

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?

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.

What is angular routing?

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.

How do I activate a route in angular?

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.

How to implement same route refresh event in angular router?

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.


2 Answers

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.

like image 137
Amit Chigadani Avatar answered Oct 20 '22 05:10

Amit Chigadani


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

like image 22
Dinesh Ghule Avatar answered Oct 20 '22 03:10

Dinesh Ghule