URLSearchParams 报错
来源:11-11 自定义参数序列化 需求分析+ 代码编写 + demo编写

慕丝8101871
2020-02-04
老师,我发现按照课件中的instanceof关键字方法判断URLSearchParams类型在运行时会有如下报错:
Uncaught (in promise) TypeError: Right-hand side of ‘instanceof’ is not an object
但是改成:
toString.call(val) === '[object URLSearchParams]'
就没有问题了,这是什么原因呀?
写回答
2回答
-
慕丝8101871
提问者
2020-02-05
import { URLSearchParams } from "url"; // 报错的写法 export function isURLSearchParams(val: any): val is URLSearchParams { return typeof val !== 'undefined' && val instanceof URLSearchParams; } //没有报错的写法 export function isURLSearchParams(val: any): val is URLSearchParams { return typeof val !== 'undefined' && toString.call(val) === '[object URLSearchParams]'; }
具体报错:
Uncaught (in promise) TypeError: Right-hand side of 'instanceof' is not an object
at isURLSearchParams (universal.ts:56)
at buildUrl (url.ts:40)
at transformUrl (dispatchRequest.ts:29)
at processConfig (dispatchRequest.ts:20)
at dispatchRequest (dispatchRequest.ts:14)
012020-02-05 -
ustbhuangyi
2020-02-04
你代码是怎么写的,贴出来看看
012020-02-05
相似问题