我正在尝试将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进行重新设计以提供对get query params的支持。

关于asp.net-mvc - Angular 2 : how to get params from URL without using Routing?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36832258/

10-11 11:27