路由角度 5 中的破折号分隔参数

2023-11-22

我想用破折号分隔 URL 中的参数,如下所示:

localhost/add/5-ninja

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


我认为按照您喜欢的方式不可能,但这是我实现该结果的建议:

  • 在你的路由配置中你声明路径:例如。/:dashed
  • 在你的组件中:

    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
      }
    }
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

路由角度 5 中的破折号分隔参数 的相关文章

随机推荐