如何使用" /:params"获取参数在Angular 2中

时间:2017-06-04 18:34:08

标签: angular angular2-routing

我想从与此类似的链接中获取参数" sample.com/product/45"使用" product /:id", 这是我的路线声明:

export const routes: Routes = [
  { path: 'product/:id', component: Product }
];

这是组件类

export class Product implements OnInit, OnDestroy {
  id: number;
  private sub: any;

  constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
       this.id = +params['id'];

       console.log(this.id); //I get undefined here
    });
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
}

当我尝试使用console.log()id时,我得到了未定义的

1 个答案:

答案 0 :(得分:4)

试试这个,

SELECT keyname, real_ip, virtual_ip, received_bytes, sent_bytes, connection_tstamp FROM connection_temporary WHERE keyname IN (SELECT keyname FROM CONNECTION)