在 React Native 中,我想对 URLSearchParams 进行抽象,所以我编写了这个类:
export class HttpSearchParamsAdapter extends URLSearchParams implements HttpSearchParams {
constructor() {
super();
}
set(param: string, val: string): void {
super.set(param, val);
}
}
但是当我构建应用程序时,出现此错误:找不到变量:URLSearchParams。我使用 es6 中的 URLSearchParams。
正如 Mrgoos 所说,URLSearchParams
iOS 不支持使用JavaScript 核心 http://trac.webkit.org/wiki/JavaScriptCore,您可以向 React Native 添加一个 polyfill。
例如,我在使用时遇到问题wretch https://github.com/elbywan/wretch,所以我在全球范围内使用了这个polyfill:url-搜索-params-polyfill https://github.com/jerrybendy/url-search-params-polyfill
// index.js
import 'url-search-params-polyfill';
更新(针对 React Native 0.59)
在React Native 0.59中,开发人员“实现”了一些方法URLSearchParams
with throw new Error('not implemented');
所以……它惨遭失败。
您可以在这里阅读该问题:https://github.com/facebook/react-native/issues/23922 https://github.com/facebook/react-native/issues/23922
我们可以使用,而不是我使用的polyfillwhatwg-url
实施和buffer
包如下所述:https://github.com/facebook/react-native/issues/23922#issuecomment-476070032 https://github.com/facebook/react-native/issues/23922#issuecomment-476070032
// index.js
import { URL, URLSearchParams } from 'whatwg-url';
import { Buffer } from 'buffer';
// react-native 0.59 added its own global URLSearchParams without implementation...
// https://github.com/facebook/react-native/blob/e6057095adfdc77ccbbff1c97b1e86b06dae340b/Libraries/Blob/URL.js#L66
// Issue: https://github.com/facebook/react-native/issues/23922
global.Buffer = Buffer;
global.URL = URL;
global.URLSearchParams = URLSearchParams;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)