问题
I want to separate my params in URL by dash like below:
localhost/add/5-ninja
in here the id is 5 and the name is ninja. When i changed the config to this:
path: '/:id-:name'
It doesn't work properly.
How can I create dash separated params in URL
回答1:
I think it's not possible the way you like but here's my suggestion to achieve that result:
- in your routes config you declare the path: for ex.
/:dashed
in your component:
import { ActivatedRoute } from '@angular/router'; class MyComponent { constructor(private _route: ActivatedRoute) { const [id, name] = _route.snapshot.params.dashed.split('-'); // you've got two variables 'id' and 'name' thanks to the array destructing } }
回答2:
You could use a custom UrlSerializer to parse the URL as you see fit.
Source
来源:https://stackoverflow.com/questions/47727014/dash-separated-params-in-routing-angular-5