找不到变量:URLSearchParams [英] Can't find variable: URLSearchParams
问题描述
在React Native中,我想对URLSearchParams进行抽象,所以我写了这个类:
In React Native, I would like to do an abstraction of URLSearchParams so I write this class:
export class HttpSearchParamsAdapter extends URLSearchParams implements HttpSearchParams {
constructor() {
super();
}
set(param: string, val: string): void {
super.set(param, val);
}
}
但是当我构建应用程序时,我有这个错误:找不到变量:URLSearchParams。我使用es6中的URLSearchParams。
But when I build the app, I have this error: Can't find variable: URLSearchParams. I use URLSearchParams from es6.
推荐答案
正如mrgoos所说, URLSearchParams
是在使用 JavaScript Core 的iOS中不支持,您可以向React Native添加polyfill。
As mrgoos said, URLSearchParams
is not supported in iOS which uses JavaScript Core, you can add a polyfill to React Native.
例如,我在使用 wretch 时出现问题,所以我在全球使用这种polyfill: url-search-params-polyfill
For example, I had a problem using wretch, so I used this polyfill globally: url-search-params-polyfill
// index.js
import 'url-search-params-polyfill';
UPDATE(对于React Native 0.59)
在React Native 0.59中,开发人员实现 URLSearchParams
的一些方法,并带有抛出新错误('未实现' );
所以...它很糟糕。
In React Native 0.59, the developers "implemented" some methods of URLSearchParams
with throw new Error('not implemented');
so... it miserably fails.
你可以在这里阅读这个问题: https://github.com/facebook/react-native/issues/23922
You can read the issue here: https://github.com/facebook/react-native/issues/23922
我们可以使用 whatwg-url
实现和缓冲区
包而不是我使用的polyfill。在此解释: https://github.com/facebook/react-native / issues / 23922 #issicecomment-476070032
Instead of the polyfill I used, we can use whatwg-url
implementation and the buffer
package as explained here: 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;
这篇关于找不到变量:URLSearchParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!