Javascript Pass获取参数

时间:2018-06-28 08:43:09

标签: javascript

我得到了存在,可以根据要求获取参数:

constructor( parentInjector:Injector ){

  let injector = ReflectiveInjector.resolveAndCreate ( [ ProductService ] );

  this.productService = injector.get(ProductService, parentInjector);
}

如何用js filterBy之后传递参数?

我的解决方案:

?filterBy=1,2,3

就我而言,我重写了get params。
示例:如果我有请求中的参数:switch(type) { case 'status': window.location.href = '?filter-status=i=' + filters; break; case 'oldFrom': window.location.href = '?oldFrom'; break; case 'oldTo': window.location.href = '?oldTo'; break; case 'newFrom': window.location.href = '?newFrom'; break; case 'newTo': window.location.href = '?newTo'; break; case 'catesel': window.location.href = '?byCategory=' + filters; break; } 并且当我单击按钮以按请求添加新过滤器时,将删除并编写新参数。我该怎么做:

byCategory=1,2,3 ...

我需要删除?byCategory=1,2,3&newFrom或通过方式如何获得参数?

2 个答案:

答案 0 :(得分:0)

要从网址中获取参数,请使用URL构造函数:

const url = new URL("?filterBy=1,2,3");
const filterBy = url.searchParams.get("filterBy");
console.log(filterBy);

如果需要获取值数组,只需将其拆分为数组

const myArrOfFilters = filterBy.split(',');
console.log(myArrOfFilters);

答案 1 :(得分:0)

使用URLSearchParams获取查询字符串值

var params = '';
var type = 'status';
var filters = '1,2';

switch (type) {
 case 'status':
  params = '?filter-status-i=' + filters;
  break;
 case 'oldFrom':
  params = '?oldFrom';
  break;
 case 'oldTo':
  params = '?oldTo';
  break;
 case 'newFrom':
  params = '?newFrom';
  break;
 case 'newTo':
  params = '?newTo';
  break;
 case 'catesel':
  params = '?byCategory=' + filters;
  break;
}
//window.location.href = params
var urlParams = new URLSearchParams(params);
console.log(urlParams.get('filter-status-i'));