Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different routes same component

I want to achieve something like this /products shows all the products and /products/:category shows all the products related to a specific category. To achieve that I have the following routes:

const productsRoutes: Routes = [   {     path: 'products',     component: ProductsComponent,     children: [       {         path: '',         component: ProductsListComponent,       },       {         path: ':category',         component: ProductsListComponent       }     ]   } ]; 

Problem

When I switch between categories, everything is fine, when I switch between all products and category products, and vice-versa, Angular redraws the components and there's a flickering.

Angular 2 Router final version doesn't have Regex,as for as I know. Is there something that I'm missing, or for now this is the only solution?

like image 606
Fabio Antunes Avatar asked Sep 28 '16 16:09

Fabio Antunes


People also ask

How do I render the same component on different routes?

Better solution would be, use componentWillReceiveProps lifecycle method in ManageClient component. Idea is whenever we render same component for two routes and switching between them, then react will not unmount-mount component, it will basically update the component only.

What are the main components of routes?

There are three main components that we use to configure routing in Angular: Routes describes the routes our application supports. RouterOutlet is a “placeholder” component that shows Angular where to put the content of each route. RouterLink directive is used to link to routes.

What is a component less routes used for?

We called such routes componentless routes. Their main purpose is to consume URL segments, provide some data to its children, and do it without instantiating any components. The parameters captured by a componentless route will be merged into their children's parameters.

What are the two elements of routes in Angular?

The first property, path , defines the URL path for the route. The second property, component , defines the component Angular should use for the corresponding path.


1 Answers

you can solve it by adding routes

const routes: Routes = [     { path: 'experience',         children: [             { path: 'pending', component: ExperienceComponent },             { path: 'requests', component: ExperienceComponent },         ] }] 

and in ExperienceComponent import

import { ActivatedRoute } from "@angular/router"; 

and in constructor add this parameter

constructor(public route: ActivatedRoute) 

and inside constructor get url

this.route.url.subscribe(params => {   console.log(params[0].path); }) 
like image 67
Mohamed Abo Elmagd Avatar answered Sep 27 '22 00:09

Mohamed Abo Elmagd