路线参数是空的

时间:2017-05-19 16:22:59

标签: angular

我有以下用户模板文件,其中包含选项卡控件

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

以及以下app.route config

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' }            
    ]
}

];

问题在于我想在&#34;中跟随&#34;像这样的组件:

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

但是参数总是空的。

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

我也试过像这样打电话给父母,但是参数仍然是真的......

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

            console.log(params);
});

2 个答案:

答案 0 :(得分:2)

用它来路由:

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 :(得分: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: ''
    ...
  }
]