1

通过访问myproject.dev/people?filter%5Bindustry%5D=finance&filter%5BstartWith%5D=a,Angular2 将 url 指向myproject.dev/people

这是我的路由配置:

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

在主组件中:

/// <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
    }
}

我仍然可以从 获取它this.router.root.lastNavigationAttempt,但这只是获取它的一种棘手方法。有更好的方法来获取 GET 参数吗?

4

3 回答 3

0

在根组件中,您可以注入路由器并订阅,然后在路由事件中从路由器获取参数,例如

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

在路由器添加的组件上,您可以RouteParams直接注入

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 示例

于 2016-04-22T05:03:06.317 回答
0

我的解决方案:当然不是最好的方法,但它有效:我假设你有这样的 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);

当然不是最好的方法,但它工作得很好:)

于 2016-04-22T10:18:30.593 回答
0

@Günter Zöchbauer 是正确的。子路由只能使用matrix parameter,不能query parameter

于 2016-04-22T14:55:56.007 回答