dash separated params in routing angular 5

谁说胖子不能爱 提交于 2019-12-22 10:27:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!