Angular 2 Router:多个路由参数 [英] Angular 2 Router: multiple route parameters

查看:192
本文介绍了Angular 2 Router:多个路由参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用该网址作为路由

I want to have a route with that url

/collections/:collectionId/books/:bookId/:title

例如

/collections/10/books/456678/my-book.html

在我的app-routing.module.ts文件中

in my app-routing.module.ts file

{path: 'collections/:collectionId/books/:bookId/:title', component: BookDetailsComponent},

是否可以不使用嵌套的路由器插座?

Is is possible without using nested router outlets?

我尝试过

this.router.navigate(['/collections',{collectionId: 10, bookId: 456678, title: "my-book.html"}]);

但出现类似

Cannot match any routes. URL Segment: 'collections;collectionId=10;bookId=456678;title=my-book.html'

如果我将参数作为数组传递,

if I pass the paramaters as an array instead,

this.router.navigate(['/collections', 10, 456678, "my-book.html"]);

我知道

Error: Cannot match any routes. URL Segment: 'collections/10/456678/my-book.html

我设法在馆藏"路线下使用书籍"子路线实现了我想要的目标,但我想在没有嵌套路线的情况下做到这一点.

I managed to achieve what I wanted using a 'books' child route under the 'collections' route, but I'd like to do it without nested routes.

谢谢

推荐答案

应该是

this.router.navigate(['/collections', '10', 'books', '456678', "my-book.html"]);

数字周围的引号不是必需的,但我认为这是个好习惯.

The quotes around the numbers are not necessary but I think it's good practice.

这篇关于Angular 2 Router:多个路由参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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