我正在尝试将 URL 参数获取到我的组件中,以便使用 externsre 服务进行搜索。我读到我可以使用 angular2 路由并使用 RouteParams 获取参数,但我认为这不是我需要的,因为我的 Angular 应用程序的第一页是 Razor View 。
我会尽力解释得更好。 我的网址看起来像: http://dev.local/listing/?city=melbourne
我在 razor View 中加载我的根组件,如下所示:
<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);
}
}
所以我期待我的组件中的“城市”获取从 razor View 传递的字符串,但它是未定义的。
我做错了什么?有没有办法在不使用路由的情况下获取参数?如果不是, Razor 的正确使用方法是什么?
最佳答案
Angular 不为此提供特殊支持。您可以使用 How can I get query string values in JavaScript?
据我所知,Location
计划进行重新设计,以提供对获取查询参数的支持,即使不使用路由器也是如此。
关于asp.net-mvc - Angular 2 : how to get params from URL without using Routing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36832258/