Angular 2:如何在不使用路由的情况下从 URL 获取参数?

Mar*_*pez 5 asp.net-mvc razor angular2-routing angular

我正在尝试将 URL 参数放入我的组件中以使用 externsre 服务进行搜索。我读到我可以使用 angular2 路由并使用 RouteParams 获取参数,但我认为这不是我需要的,因为我的 angular 应用程序的第一页是 Razor 视图。

我会尽量解释得更好。我的 URL 看起来像: http://dev.local/listing/?city=melbourne

我在剃刀视图中加载我的根组件,如下所示:

<house-list city = "@Request.Params["city"]"></house-list>
Run Code Online (Sandbox Code Playgroud)

然后在我的根组件上,我有:

export class AppComponent implements OnInit {
    constructor(private _cityService: CityService) { }

    response: any;
    @Input() city: any;

    ngOnInit() {
        this.getHouses();
    }

    getHouses() {
        this.__cityService.getHousesByCity(this.city)
            .subscribe(data => this.response = data);

    }
}
Run Code Online (Sandbox Code Playgroud)

所以我期待我的组件中的“城市”获取从剃刀视图传递的字符串,但它是未定义的。

我究竟做错了什么?有没有办法在不使用路由的情况下获取参数?如果没有,使用剃须刀的正确方法是什么?

Gün*_*uer 4

Angular 没有为此提供特殊支持。您可以使用如何在 JavaScript 中获取查询字符串值?

据我所知,Location计划进行重新设计,以提供对获取查询参数的支持,即使不使用路由器。