dash separated params in routing angular 5

2019-05-28 23:04发布

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

2条回答
狗以群分
2楼-- · 2019-05-28 23:52

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
      }
    }
    
查看更多
疯言疯语
3楼-- · 2019-05-28 23:58

You could use a custom UrlSerializer to parse the URL as you see fit.

Source

查看更多
登录 后发表回答