2016-11-16 60 views
0

我有这样无法获取路线PARAMS在angular2

<a [routerLink]=" ['../projects', project.id] "> 

的路线,当我尝试摆脱这个URL路由参数,我什么也得不到。

我进口:

import { Component} from '@angular/core'; 
import { ActivatedRoute } from '@angular/router'; 

我的构造函数:

constructor(private route: ActivatedRoute) {} 

我在INIT:

ngOnInit() { 

    this.sub = this.route.params.subscribe(params => { 
     console.log(params); //there i get only empty Object with `__proto__` 
    }); 
    } 
} 

谁能帮助我?

+2

请提供更多信息。理想情况下,允许重现的Plunker。读取参数通常只是起作用,因此问题可能在其他地方(路由配置,使用的组件,...) –

+0

Sory这是我的错误。我选择一种不好的方式。现在我实现了保存project.id的服务,然后在头文件中使用它。 –

+0

这没有帮助。如果您提供允许重现它的Plunker可能会更容易。 –

回答

1

使用快照获取路由参数。

let id = +this.route.snapshot.params['id']; 
//do something here