2

This is my current router:

const routes: Routes = [
  { path: '',  component: HomeComponent },
  { path: 'filter',  component: FiltersPageComponent},
  { path: 'filter/:continent',  component: FiltersPageComponent},
  { path: '**', canActivate: [RouteLoader], component: ErrorComponent},
];

I have a dynamic url that is created when a user searches for multiple destinations.

Example of url: localhost:4200/filter/africa/botswana/europe/spain/asia/china

Can I create 1 path that will accept this url without making multiple paths like below?

{ path: 'filter/:test/:test2/:test3/:test4/:test5/:test6',  component: FiltersPageComponent}
2
  • Not sure what you want to achieve with a URL that looks like that Commented Nov 21, 2017 at 11:47
  • A user can search for multiple destinations. I put them in the url to give these destinations to another page so I can do a get request with them on that other page. But I asked this question to see if it is possible to create a dynamic path so i wouldn't have to use a limit on the amount of destinations a user can search for. Commented Nov 21, 2017 at 12:46

4 Answers 4

2

The solution I ended up using was this:

{ path: 'filter',  component: FiltersPageComponent,
  children: [
    { path: '**', component: FiltersPageComponent}
  ]
},

The only problem I can think of when using this method is that the user can randomly put anything after filter/ in the url and it will still redirect to the filtersPageComponent.

Sign up to request clarification or add additional context in comments.

Comments

1

With path params it is impossible (and also wrong), but you should be able to do this with query params like this:

localhost:4200/yourroute?filter=africa&filter=botswana&filter=europe&filter=spain

Query params and fragments

Comments

0

No you cant. You cannot have duplicate params in your routes. IF you do so, you can only get the last data that has duplicate in the url. In your case, it will return only asia and china

Comments

0

Angular's paramMap should enable this.

You can subscribe to the url parameters and run what you wish on each route respectively. See the example below:

this.route.paramMap.subscribe(params => {
    if (params.get('username'))) {
        // Route to particular path...
        // Or do something else...
    }
});

Here are some additional resources:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.