Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2中URLSearchParams()存在奇怪的问题_Url_Angular - Fatal编程技术网

Angular2中URLSearchParams()存在奇怪的问题

Angular2中URLSearchParams()存在奇怪的问题,url,angular,Url,Angular,我发现URLSearchParams()出现了一个奇怪的问题,它只有在定义var search时才起作用(即,如果我将变量名更改为var otherName),它就不起作用 但是,由于某些原因,它仅在名称为搜索时起作用!!这完全不符合逻辑 知道这里发生了什么吗 constructor(http) { this.http = http; this.genre = null; this.dishes = null; //this one works fine var sea

我发现
URLSearchParams()
出现了一个奇怪的问题,它只有在定义
var search
时才起作用(即,如果我将变量名更改为
var otherName
),它就不起作用

但是,由于某些原因,它仅在名称为
搜索时起作用
!!这完全不符合逻辑

知道这里发生了什么吗

  constructor(http) {
  this.http = http;
  this.genre = null;
  this.dishes = null;

  //this one works fine
  var search = new URLSearchParams();
  search.set('order', '-ordersNo');

  //Here is the issue (to make it work, I need to remove the previous search declaration, and rename the below var limit to search)
  var limit = new URLSearchParams();
  limit.set('limit', '2');

  this.http.get('https://example.com/classes/Mn', { limit }).subscribe(data => {
  this.dishes = data.json().results;
  });


  this.http.get('https://example.com/classes/Genre',{ search }).subscribe(data => {
  this.genre = data.json().results;
  });

我想你真的想要这样:

{ search: search }

//and 

{ search: limit }
http get方法的
选项
参数必须具有以下签名:

export interface RequestOptionsArgs {
    url?: string;
    method?: string | RequestMethod;
    search?: string | URLSearchParams;
    headers?: Headers;
    body?: string;
}

但是
{limit}
实际上与
{limit:limit}

是一样的,我想你真的想要这样:

{ search: search }

//and 

{ search: limit }
http get方法的
选项
参数必须具有以下签名:

export interface RequestOptionsArgs {
    url?: string;
    method?: string | RequestMethod;
    search?: string | URLSearchParams;
    headers?: Headers;
    body?: string;
}

但是
{limit}
实际上与
{limit:limit}
实际上,它是JavaScript的一个快捷方式:
{search}
相当于
{search:search}
{limit:limit}/code>相当于
{limit search:limit}


对于第一种情况,它可以正常工作,因为Angular2需要一个
search
属性来定义查询参数。如果你想使用
limit
变量,你不能使用这个快捷方式,你需要使用下面的:
{search:limit}
事实上,它是JavaScript的一个快捷方式:
{search}
相当于
{search:search}
{limit code}
{limit limit:limit}


对于第一种情况,它可以正常工作,因为Angular2需要一个
search
属性来定义查询参数。如果您想使用
limit
变量,您不能使用此快捷方式,您需要使用以下选项:
{search:limit}

您能详细说明一下吗?您的建议有效,但我仍然不明白为什么当我将search更改为另一个名称时,它会停止工作!真奇怪!!你能详细说明一下吗?你的建议行得通,但我还是不明白为什么当我把搜索改成另一个名字时它就停止工作了!真奇怪!!{search:limit}当然有效!但是,让我们看看另一个场景,在调用limit之后删除'var search'只留下'var limit',它不起作用!搜索是保留的还是什么?要传递给Angular2的对象的结构如@kemsky所述在
请求选项args
界面中描述<代码>限制
不是Angular2支持的属性…{search:limit}当然有效!但是,让我们看看另一个场景,在调用limit之后删除'var search'只留下'var limit',它不起作用!搜索是保留的还是什么?要传递给Angular2的对象的结构如@kemsky所述在
请求选项args
界面中描述<代码>限制不是Angular2支持的属性。。。