如何从角度2中获取url的值

Mal*_*iri 2 typescript angular

我试图获得价值,但它无法正常工作.下面是我的网址,我想1从网址获取数字

https://开头本地主机:44300 /门户#/用户编辑/ 1

constructor(private router: Router,
    private userService: UserService) {

    this.router
        .routerState
        .queryParams
        .subscribe(params => {
            this.id = params['id'];
        });
Run Code Online (Sandbox Code Playgroud)

mic*_*yks 5

您应该注入ActivatedRoute来获取路由参数,如下所示,

constructor(private router: Router,
    private userService: UserService,
    private route: ActivatedRoute,) {  //<----- here

    this.route.params.subscribe(params => {
            this.id = +params['id'];   //<----- + sign converts string value to number
});
Run Code Online (Sandbox Code Playgroud)

在此处阅读更多内容:https://angular.io/docs/ts/latest/guide/router.html#!#route-parameters