2017-05-19 49 views
1

我有以下的用户模板文件,其中包含标签控件路线params为空

<nav md-tab-nav-bar>    
      <a class="tab-label" md-tab-link [routerLink]="'following'" routerLinkActive #following="routerLinkActive" [active]="following.isActive"> 
      Following 
      </a> 

及以下app.route配置

export const ROUTES: Routes = [ 
{ 
    path: 'user/:id', loadChildren: './user/user.module#UserModule' 
}, 

及以下user.route config

const ROUTES: Routes = [ 
{ 
    path: '', component: UserComponent, 
    children: [ 

     { path: 'following', loadChildren: './following/following.module#FollowingModule' }    
    ] 
} 

];

的问题是,我想要得到的ID在这样的“下面的”组件:

this.route.params.subscribe(params => { 
      console.error(params); 
     }); 

但params为总是空的。

我是否需要以某种方式指定user.route中的id参数?

我也打过电话这样的父母,但params为仍然empy ...

this.subRoute = this.route.parent.params.subscribe(params => { 

      console.log(params); 
}); 
+0

你能过去在这里,你试图URL的例子吗? – developer033

+1

嗨@ developer033这里是一个例子http:// localhost:3000/user/5836aadf7911f7725ce3cd2b /关注 – doorman

+0

嗨,我的解决方案正在工作。请在这里添加你的plunker网址。 – Houtan

回答

0

使用该路由:

this.router.navigate(['/productlist'], { queryParams: { filter: JSON.stringify(this.product) } }); 

和另一个组件:

import { Component, OnInit } from '@angular/core'; 
import { Router, ActivatedRoute, Params } from '@angular/router'; 
. 
. 
. 
export class ProductListComponent implements OnInit { 

    constructor(private route: ActivatedRoute, private router: Router) { 
    } 

    public ngOnInit() { 
     this.route 
      .queryParams 
      .subscribe(params => { 
       let filterParam = JSON.parse(params['filter']); 
       console.log(filterParam); 
      }); 
    } 
} 
+1

反对票的原因是什么? – ceebreenk

+0

也许他需要SO –

2

我是否需要以某种方式指定user.route中的id参数?

是的,你需要在路径指定参数:

const routes: Routes = [ 
{ 
    path: '', component: UserComponent, 
    children: [  
    { 
     loadChildren: './following/following.module#FollowingModule', 
     path: ':parent_id/following' // <- Here 
    } 
    ] 
} 

所以,某处“下面的” 组件

const parentId = this.activatedRoute.snapshot.paramMap.get('parent_id'); 

PS :我是假设你有一个这样的配置路径(在“下面的”组件):

const routes: Routes = [ 
    { 
    component: FollowingComponent, path: '' 
    ... 
    } 
] 
+0

Hi @ developer033谢谢你的建议。但是,如果我添加parent_id,我路由到根可能是因为app.routes已经定义:父参数所需的id参数。 – doorman

+0

嘿,我不明白你在说什么..你测试了我的解决方案吗? – developer033

+0

是的,所以我有一切设置像你的建议。另外,我在app.routes中有'user /:id'路径。现在如果我导航到http:// localhost:3000/user/5836aadf7911f7725ce3cd2b /,那么导航到http:// localhost:3000可能是因为它找不到任何匹配项。如果我从路径中删除parent_id:':parent_id/following',它可以正常工作 – doorman