Angular 如何在Nativescript中将参数从同级路由传递到子路由
我使用Angular 如何在Nativescript中将参数从同级路由传递到子路由,angular,nativescript,angular2-nativescript,nativescript-angular,Angular,Nativescript,Angular2 Nativescript,Nativescript Angular,我使用作为我的应用程序的主要出口 在其中一个pages.html中,我有几个导航按钮和一个,因为我想将单击这些按钮的结果呈现给 在路由级别,页面具有子路由: const groceryListRoutes: Routes = [ { path: 'grocery-lists', component: GroceryListsComponent, }, { path: 'grocery-list/:id', component: GroceryListC
作为我的应用程序的主要出口
在其中一个pages.html中,我有几个导航按钮和一个
,因为我想将单击这些按钮的结果呈现给
在路由级别,页面具有子路由:
const groceryListRoutes: Routes = [
{
path: 'grocery-lists',
component: GroceryListsComponent,
},
{
path: 'grocery-list/:id',
component: GroceryListComponent,
children: [
{
path: '',
component: GroceryListProductsComponent,
},
{
path: 'product-categories',
component: ProductCategoriesComponent,
},
{
path: 'products',
component: ProductsComponent,
},
],
},
从列出所有杂货清单的页面,我链接到显示特定杂货清单信息的页面,使用[nsRouterLink]
如下所示:
<Label [text]="item.name" margin="10" [nsRouterLink]="['/grocery-list', groceryListId]"></Label>
<Label text="Next" width="80" height="80" [nsRouterLink]="['../products/', 22]"></Label>
constructor(private pageRoute: PageRoute, private router: Router){
this.pageRoute.activatedRoute
.switchMap(activatedRoute => activatedRoute.params)
.forEach((params) => {
this.zzz= params['zzz'];
});
}
constructor(private pageRoute: PageRoute, private router: Router){
this.pageRoute.activatedRoute
.switchMap(activatedRoute => activatedRoute.children[0].paramMap)
.forEach((params) => {
this.zzz= params['params']['zzz'];
});
}
没有结果
我在目标组件上获得如下参数:
<Label [text]="item.name" margin="10" [nsRouterLink]="['/grocery-list', groceryListId]"></Label>
<Label text="Next" width="80" height="80" [nsRouterLink]="['../products/', 22]"></Label>
constructor(private pageRoute: PageRoute, private router: Router){
this.pageRoute.activatedRoute
.switchMap(activatedRoute => activatedRoute.params)
.forEach((params) => {
this.zzz= params['zzz'];
});
}
constructor(private pageRoute: PageRoute, private router: Router){
this.pageRoute.activatedRoute
.switchMap(activatedRoute => activatedRoute.children[0].paramMap)
.forEach((params) => {
this.zzz= params['params']['zzz'];
});
}
但是当我试图提醒()变量this.zzz时,它说:未定义
ngOnInit(){
alert(this.zzz);
}
所以我的问题是,如何将参数从Nativescript中的同级路由传递到子路由?因为当我在两个根路由之间传递参数时,我没有问题
提前谢谢。解决了这个问题
我应该从从PageRoute获得的activatedRoute访问子路由。所以构造函数应该是这样的:
<Label [text]="item.name" margin="10" [nsRouterLink]="['/grocery-list', groceryListId]"></Label>
<Label text="Next" width="80" height="80" [nsRouterLink]="['../products/', 22]"></Label>
constructor(private pageRoute: PageRoute, private router: Router){
this.pageRoute.activatedRoute
.switchMap(activatedRoute => activatedRoute.params)
.forEach((params) => {
this.zzz= params['zzz'];
});
}
constructor(private pageRoute: PageRoute, private router: Router){
this.pageRoute.activatedRoute
.switchMap(activatedRoute => activatedRoute.children[0].paramMap)
.forEach((params) => {
this.zzz= params['params']['zzz'];
});
}
我使用以下命令从子路由访问参数:
activatedRoute.children[0].paramMap