布线角5中的破折号分隔的参数 [英] dash separated params in routing angular 5

查看:56
本文介绍了布线角5中的破折号分隔的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用以下破折号分隔URL中的参数:

I want to separate my params in URL by dash like below:

localhost/add/5-ninja

在这里,id为5,名称为ninja.当我将配置更改为此: path: '/:id-:name' 它不能正常工作. 如何在URL中创建破折号分隔的参数

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

推荐答案

我认为这不是您喜欢的方式,但这是我建议达到的结果:

I think it's not possible the way you like but here's my suggestion to achieve that result:

  • 在路由配置中,您声明路径:例如. /:dashed
  • 在您的组件中:

  • 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
  }
}

这篇关于布线角5中的破折号分隔的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆