如何使用routerLink将params传递给一个组件?

时间:2018-06-04 11:40:51

标签: angular angular5 angular2-routing

我遇到了使用routerLink传递/获取参数的不可能性。

我有app.component.ts / html:

 <header></header>
<router-outlet></router-outlet>
<footer class="flux-container">
  ...
  <a routerLink="/info/1" routerLinkActive="active">Privacy Policy</a>
  <a routerLink="/info/2" routerLinkActive="active">Terms & Conditions</a>
  ...
</footer>

和info.component.ts:

&#13;
&#13;
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-info',
  templateUrl: './info.component.html',
  styleUrls: ['./info.component.css']
})
export class InfoComponent implements OnInit {
  order : number;
  constructor(private activatedRoute: ActivatedRoute) { }

  ngOnInit() {
    this.order = this.activatedRoute.snapshot.params.id;
    console.log('this.activatedRoute.snapshot.params ', this.activatedRoute.snapshot.params);
  }
}
&#13;
&#13;
&#13;

routing.ts

&#13;
&#13;
{
    path         : 'info/:id',
    canActivate  : [AccountGuard, AppEnter],
    pathMatch    : 'full',
    component    : InfoComponent
  },
&#13;
&#13;
&#13;

通过cliking / info / 1或/ info / 2

我无法获得this.activatedRoute.snapshot.params.id并仅使用刷新页面来使用它。只需简单的移动。

请帮助

1 个答案:

答案 0 :(得分:1)

order$: Observable<string>;

this.order$ = this.activatedRoute.paramMap.pipe(map(resp => resp.get("id")));