我想用破折号分隔 URL 中的参数,如下所示:
localhost/add/5-ninja
这里的 id 是 5,名字是 ninja。当我将配置更改为:path: '/:id-:name'它无法正常工作。 如何在 URL 中创建破折号分隔的参数
path: '/:id-:name'
我认为按照您喜欢的方式不可能,但这是我实现该结果的建议:
/: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 } }