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

Ala*_*412 5 angular-ui-router angular

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

localhost/add/5-ninja
Run Code Online (Sandbox Code Playgroud)

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

Bro*_*rec 2

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

  • 在你的路由配置中你声明路径:例如。/: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
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)