How to get GET paramater in Angular2?

混江龙づ霸主 提交于 2019-12-04 05:16:07

问题


By accessing myproject.dev/people?filter%5Bindustry%5D=finance&filter%5BstartWith%5D=a, Angular2 point the url to myproject.dev/people

Here is my RouteConfig:

@RouteConfig([
    {
        path: '/people',
        name: config.route.main,
        component: MainComponent,
        useAsDefault: true
    }
])

In MainComponent:

/// <reference path="../../../typings/angular2.d.ts" />

import {Component, Injector} from 'angular2/core';
import {ROUTER_DIRECTIVES, Router, RouteParams} from 'angular2/router';
import {BaseResourceComponent} from '../../Component/BaseResourceComponent';
import {Status as MainStatus} from '../../reusable/modules/status.svc';

import {Status} from '../../reusable/modules/status.svc';
import {Config} from "./Config";

import URI from 'urijs';

export class MainComponent extends BaseResourceComponent {
    constructor(config: Config, status: Status, mainStatus: MainStatus, private router: Router, private routeParams: RouteParams) {
        super(config, status, mainStatus);
    }

    onInit() {
        var path = new URI(window.location.href);
        path.setQuery('filter[industry]', 'fashion');
        path.setQuery('filter[startWith]', 'a');

        console.log(path);
        console.log(this.router);
        //this.router.root.lastNavigationAttempt = "/people?filter%5Bindustry%5D=finance&filter%5BstartWith%5D=a"

        console.log(this.routeParams);
        // this.routeParams returns {params: Object}
        // this.routeParams.params.get('filter') return null
    }
}

I still can get it from this.router.root.lastNavigationAttempt, but this is kind of tricky way to get it only. Any better way to get the GET parameters?


回答1:


In the root component you can inject the router and subscribe, then on route events get the params from the router like

export class AppComponent {
  constructor(private router:Router) {
    router.subscribe(route => {
      console.debug(this.router.currentInstruction.component.params);
    });
  }
}

On components added by the router you can inject RouteParams directly like

export class Other{
    constructor(private routeParams: RouteParams) {
    console.debug(this.routeParams);
    console.log(this.routeParams.get('filter_industry'));
    console.log(this.routeParams.get('filter_start_with'));
  }
}

Plunker example




回答2:


My solution : certainly not the best way t odo it but it work : I assume that you have this kin of url : http://localhost:8080/contextPath/index.html?login=true#token_type=Bearer&expires_in=9999&access_token=xxxXXXXXxxx

//get base url to get the token
if

(this.location == "")

{
    console.log("traitement location");
    this.location = location.href;
}

//extract all :
if (this.location != "") {
    console.log("traitement token");
    this.login = this.location.split("?")[0].split("=")[1];
    this.token_type = this.location.split("?")[1].split("#")[1].split("&")[0].split("=")[1];
    this.expire_in = +this.location.split("?")[1].split("#")[1].split("&")[1].split("=")[1];
    this.setLocalDateValid((this.expire_in + this.nowDate()).toString());
    this.token = this.location.split("?")[1].split("#")[1].split("&")[2].split("=")[1];
}

// then store it 
this.setLocalToken(this.token);

Certainly not the best way to do it but it work perfectly well :)




回答3:


@Günter Zöchbauer is correct. Child route can only use matrix parameter but not query parameter.



来源:https://stackoverflow.com/questions/36784822/how-to-get-get-paramater-in-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!