Angular 为一个管段声明多个管线参数
直接进入这个问题。是否有一种方法可以为一个管段声明多个管线参数,如以下管线: 路线:Angular 为一个管段声明多个管线参数,angular,angular-ui-router,angular-routing,angular-router,angular8,Angular,Angular Ui Router,Angular Routing,Angular Router,Angular8,直接进入这个问题。是否有一种方法可以为一个管段声明多个管线参数,如以下管线: 路线: { path: 'planlist/:departure_:destination/:date', component: ReservationComponent }, /* Output */ { "departure": "tokyo", "destination": "newyork", "date": "2019-09-27" } ReservationCom
{
path: 'planlist/:departure_:destination/:date',
component: ReservationComponent
},
/* Output */
{
"departure": "tokyo",
"destination": "newyork",
"date": "2019-09-27"
}
ReservationComponent中的:
import { ActivatedRoute } from '@angular/router';
/* ... */
console.log(this.route.snapshot.params)
/* Output */
{
"departure_:destination": "tokyo_newyork",
"date": "2019-09-27"
}
我对输出的期望:
{
path: 'planlist/:departure_:destination/:date',
component: ReservationComponent
},
/* Output */
{
"departure": "tokyo",
"destination": "newyork",
"date": "2019-09-27"
}
这个问题有什么解决办法吗?只是你路上的一个小问题。这样修改,
{
path: 'planlist/:departure/:destination/:date',
component: ReservationComponent
}
如果您是从TS路由
this.router.navigate(['planlist', departure, destination, date]);
或者从HTML
routerLink="/planlist/{{departure}}/{{destination}}/{{date}}"
只是路线上的一个小问题。这样修改,
{
path: 'planlist/:departure/:destination/:date',
component: ReservationComponent
}
如果您是从TS路由
this.router.navigate(['planlist', departure, destination, date]);
或者从HTML
routerLink="/planlist/{{departure}}/{{destination}}/{{date}}"
是的,你可以。您需要纠正的第一件事是使用路径捕获变量的方法: 你有:
path: 'planlist/:departure_:destination/:date',
正确的方法是:
path: 'planlist/:departure/:destination/:date',
您应该做的第二件事是按如下方式调用该方法:
router.navigate(['planlist', departure, destination, date]);
router.navigate(['planlist'], { queryParams: { departure, destination, date } });
并获取变量的值:
const departure= this.route.snapshot.params['departure'];
const destination= this.route.snapshot.params['destination'];
const date= this.route.snapshot.params['date'];
是的,你可以。您需要纠正的第一件事是使用路径捕获变量的方法: 你有:
path: 'planlist/:departure_:destination/:date',
正确的方法是:
path: 'planlist/:departure/:destination/:date',
您应该做的第二件事是按如下方式调用该方法:
router.navigate(['planlist', departure, destination, date]);
router.navigate(['planlist'], { queryParams: { departure, destination, date } });
并获取变量的值:
const departure= this.route.snapshot.params['departure'];
const destination= this.route.snapshot.params['destination'];
const date= this.route.snapshot.params['date'];
是的,你可以。您可以使用queryParams功能完成此操作 为此,您需要更正以下路径:
path: 'planlist',
发送queryParams对象中的所有变量,如下所示:
router.navigate(['planlist', departure, destination, date]);
router.navigate(['planlist'], { queryParams: { departure, destination, date } });
然后像这样抓住他们:
this.route
.queryParamMap
.pipe(
map(params => params.getAll() || [])
).subscribe((params) => {
if (!!params.length) {
this.planlistVars= params;
}
}).unsubscribe();
关键是queryParamMap,您可以在谷歌或angular的官方网站上搜索更多信息。
是的,你可以。您可以使用queryParams功能完成此操作 为此,您需要更正以下路径:
path: 'planlist',
发送queryParams对象中的所有变量,如下所示:
router.navigate(['planlist', departure, destination, date]);
router.navigate(['planlist'], { queryParams: { departure, destination, date } });
然后像这样抓住他们:
this.route
.queryParamMap
.pipe(
map(params => params.getAll() || [])
).subscribe((params) => {
if (!!params.length) {
this.planlistVars= params;
}
}).unsubscribe();
关键是queryParamMap,您可以在谷歌或angular的官方网站上搜索更多信息。
作为我的问题标题:在一个段/片段中声明多个路由参数,我的朋友。不是多个段/fragments@InOrderToLive它就是这样做的。出发、目的地和日期将成为planlist路线中的参数。你的问题是,你把出发地点和目的地合并成了一个下划线。我想问你是否有解决办法:)@InOrderLive是的。你试过我的解决方案吗?它确实满足您的确切要求。正如我的问题标题:在一个段/片段中声明多个路由参数,我的朋友。不是多个段/fragments@InOrderToLive它就是这样做的。出发、目的地和日期将成为planlist路线中的参数。你的问题是,你把出发地点和目的地合并成了一个下划线。我想问你是否有解决办法:)@InOrderLive是的。你试过我的解决方案吗?它确实满足您的确切要求。正如我的问题标题:在一个段/片段中声明多个路由参数,我的朋友。我的问题标题不是多个分段/分段:在一个分段/分段中声明多个路由参数我的朋友。不是多个段/片段