简体   繁体   English

Angular 2:如何在不使用Routing的情况下从URL获取params?

[英]Angular 2: how to get params from URL without using Routing?

I'm trying to get the URL parameters into my component to search using an externsre service. 我正在尝试将URL参数放入我的组件中以使用externsre服务进行搜索。 I read I can use angular2 routes and get params using RouteParams but I don't think that's what I need because the first page of my angular application is a Razor view. 我读过我可以使用angular2路由并使用RouteParams获取参数但我认为这不是我需要的,因为我的角应用程序的第一页是Razor视图。

I'll try to explain better. 我会试着更好地解释一下。 My URL looks like: http://dev.local/listing/?city=melbourne 我的网址如下: http://dev.local/listing/?city = melbourne

I'm loading my root component in a razor view with something like: 我正在剃刀视图中加载我的根组件,例如:

<house-list city = "@Request.Params["city"]"></house-list>

then on my root component I have: 然后在我的根组件上我有:

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);

    }
}

So I was expecting that the 'city' in my component gets the string passed from the razor view but it's undefined. 所以我期待我的组件中的“城市”获取从剃刀视图传递的字符串,但它未定义。

What am I doing wrong? 我究竟做错了什么? Is there a way of getting the params without using the routing? 有没有办法在不使用路由的情况下获取params? If not, what is the right way of using razor? 如果没有,使用剃须刀的正确方法是什么?

Angular doesn't provide special support for this. Angular没有为此提供特殊支持。 You can use How can I get query string values in JavaScript? 您可以使用如何在JavaScript中获取查询字符串值?

From what I have seen Location is planned to be reworked to provide support for get query params even without using the router. 从我所看到的Location计划进行重新设计,以便即使不使用路由器也可以获得查询参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM