如何在angular2中为新的@ Angular /路由器获取RouteParams - 似乎不存在
这是我目前的代码:
import {Component} from '@angular/core';
import {Router, ROUTER_DIRECTIVES, RouteParams} from '@angular/router';
@Component({
moduleId: module.id,
selector: 'NewJob',
templateUrl: 'newJob.html',
directives: [ROUTER_DIRECTIVES, Footer]
})
export class NewJob {
router: Router;
constructor(_router: Router, _params: RouteParams){
let slug = _params.get('slug');
}
}
答案 0 :(得分:9)
RouteSegment
不再存在。您可以使用ActivatedRoute
导入:
import {ActivatedRoute} from "@angular/router";
构造强>
constructor(private _routeParams: ActivatedRoute){}
获取参数:
this._routeParams.params.subscribe(params => {
this.id = params['id'];
});
答案 1 :(得分:3)
仅< = RC2
您使用RouteSegment。 https://angular.io/docs/ts/latest/api/router/index/RouteSegment-class.html
您可以使用getParam(param: string) : string
来获取所需的参数。