Angular 2: 如何在不使用路由的情况下从 URL 获取参数?
Angular 2: how to get params from URL without using Routing?
我正在尝试将 URL 参数放入我的组件中以使用 externsre 服务进行搜索。我读到我可以使用 angular2 路由并使用 RouteParams 获取参数,但我认为这不是我需要的,因为我的 angular 应用程序的第一页是 Razor 视图。
我会尽力解释得更好。
我的 URL 看起来像:
http://dev.local/listing/?city=melbourne
我在 razor 视图中加载我的根组件,如下所示:
<house-list city = "@Request.Params["city"]"></house-list>
然后在我的根组件上我有:
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);
}
}
所以我期待我的组件中的 'city' 获取从 razor 视图传递的字符串,但它是未定义的。
我做错了什么?有没有办法在不使用路由的情况下获取参数?如果不是,剃须刀的正确使用方法是什么?
Angular 不为此提供特殊支持。您可以使用 How can I get query string values in JavaScript?
据我所知,Location
计划重新设计以提供对获取查询参数的支持,即使不使用路由器也是如此。
我正在尝试将 URL 参数放入我的组件中以使用 externsre 服务进行搜索。我读到我可以使用 angular2 路由并使用 RouteParams 获取参数,但我认为这不是我需要的,因为我的 angular 应用程序的第一页是 Razor 视图。
我会尽力解释得更好。 我的 URL 看起来像: http://dev.local/listing/?city=melbourne
我在 razor 视图中加载我的根组件,如下所示:
<house-list city = "@Request.Params["city"]"></house-list>
然后在我的根组件上我有:
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);
}
}
所以我期待我的组件中的 'city' 获取从 razor 视图传递的字符串,但它是未定义的。
我做错了什么?有没有办法在不使用路由的情况下获取参数?如果不是,剃须刀的正确使用方法是什么?
Angular 不为此提供特殊支持。您可以使用 How can I get query string values in JavaScript?
据我所知,Location
计划重新设计以提供对获取查询参数的支持,即使不使用路由器也是如此。