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?
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.
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.
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.
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.
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); })
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