反应路由器V4-未捕获的TypeError:location.search.charAt不是函数

时间:2018-09-07 10:10:38

标签: reactjs react-router

由于React Router从“查询”更改为“搜索”。 我使用以下代码从搜索查询

查询功能:

ERROR in src/app/geo/geo.component.ts(335,11): error TS2345: Argument
 of type '{ "type": string; "features": ({ "type": string; "geometry":
{ "type": string; "coordinates": num...' is not assignable to
 parameter of type 'GeoJsonObject'.   Property 'type' is missing in
type '{ "type": string; "features": ({ "type": string; "geometry": {
"type": string; "coordinates": num...'.

搜索参数:

  setFilter(query) {
        this.props.history.push({ pathname: this.props.location.pathname, search: query });
  }

但是出现以下错误:

 applyFilter() {
    const newFilter = {};
    if (this.state.content) {
        newFilter.content = this.state.content;
    }
    if (this.state.fromDate) {
        newFilter.fromDate = this.state.fromDate;
    }
    if (this.state.toDate) {
        newFilter.toDate = this.state.toDate;
        }
    this.props.setFilter(newFilter);
  }

如何解决此问题?

3 个答案:

答案 0 :(得分:2)

其他对我有用的是:

  setFilter(query) {
        const searchParamsObject = new URLSearchParams(query);
        this.props.history.push({ pathname: this.props.location.pathname, search: searchParamsObject.toString() });
  }

答案 1 :(得分:1)

您必须按照docs的说明传递字符串以搜索属性,而不是对象

答案 2 :(得分:1)

如评论中所述。您需要删除查询对象才能解决此问题

setFilter(query) {
    this.props.history.push({ pathname: this.props.location.pathname});
    //use query object here or wherever required
}