以角度2传递和读取查询参数

时间:2017-06-23 11:34:13

标签: angular

例如,如何以角度2传递和读取查询参数 http://localhost:3000/index.html?page=235http://localhost:300/?page=235获得235?

2 个答案:

答案 0 :(得分:0)

你可以这样得到参数​​。

示例

 userId: number;
     constructor(private route: ActivatedRoute,
                    private router: Router) {
        }

        ngOnInit(): void {
            this.subscription = this.route.params.subscribe(
                (params: any) => {
                    if (params.hasOwnProperty('id')) {
                        this.userId = +params.id;
                    }
                }
            );
        }

有关详情,请参阅router

答案 1 :(得分:0)

试试这个。

import {Component,OnInit} from '@angular/core';
import {Router, ActivatedRoute, Params} from '@angular/router';

@Component({...})
export class MyComponent implements OnInit{

  constructor(private activatedRoute: ActivatedRoute) {}

  ngOnInit() {
    // subscribe to params
    this.activatedRoute.params.subscribe((params: Params) => {
        let page= params['page'];
        console.log(page);
      });
  }

}