angular - Angular2 router: matching any url, including subdirectories -


i'm updating old website angular2, , 1 of prerequisites urls must still match.

it's beginning of development , i'm working on router.

so far, have create homepage, productpage , errorpage.

the "old" links product page : /category/subcategory/some/eventual/other/things/productname-id.html

i've copied matan shukry's complex url matcher, , here's current code :

import { complexurlmatcher } '../../functions/complex.url.matcher';  const approutes: routes = [     {         path:      '',         component: homepagecomponent,     },     {         matcher:   complexurlmatcher('product', /.*/), // permissive regex here, in order test         component: productcomponent,     },     {         path:      '**',         component: error404component,     }, ]; 

my current problem can match anything, provided url not have slash.

  • /abcdefg.html works (loads productcomponent)
  • /a/b/c/d/e/f/g.html doesn't (loads error404component)
  • /a/bcdefg.html doesn't neither

how can make these urls match full paths (including slashes ?).

it better go parameters in angular can define parameters route

{ path: 'hero/:a/:b/:c/:d/:e/:f/:g', component: herodetailcomponent }, { path: 'hero/:a/:b/:c/:d/:e/:f', component: herodetailcomponent }, { path: 'hero/:a/:b/:c/:d/:e', component: herodetailcomponent }, { path: 'hero/:a/:b/:c/:d', component: herodetailcomponent }, { path: 'hero/:a/:b/:c', component: herodetailcomponent }, { path: 'hero/:a/:b', component: herodetailcomponent }, { path: 'hero/:a', component: herodetailcomponent }, { path: 'hero', component: herodetailcomponent }, 

then subscribe parammap

herodetailcomponent   import { activatedroute } '@angular/router';  constructor(     private route: activatedroute ) { }  this.route.parammap.subscribe(params => {   console.log(params); }); 

you parameters in subscription , operations

here id parameter. same way can pass multiple parameters route create route structure

https://angular.io/guide/router#route-parameters-required-or-optional go link more details


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -