url.pathname或url.searchParams不是android react native中的函数

8yoxcaq7  于 2023-04-04  发布在  Android
关注(0)|答案(2)|浏览(98)

我在android react native项目工作,我得到了深度链接的URL。我试图从以下URL中找到路径名和搜索参数:https://test.xxx.com/test/test?test=moduleName .
var url = URL(“https://test.xxx.com/test/test?test=moduleName“);console.log(url.pathName);
代码给出以下异常“错误:未实现”。我也从节点安装了URL包。我缺少了什么?

g6baxovj

g6baxovj1#

看看这个URL polyfill for React Native

import { URL, URLSearchParams } from 'react-native-url-polyfill';

const url = new URL("https://test.xxx.com/test/test?test=moduleName");
const searchParams = new URLSearchParams('q=GitHub');

另外请注意-React Native runs within the JavascriptCore engine,而Node.js/Web在v8 Javascript引擎中运行。
Node.js提供了自己的专有功能- streams,crypto,http等。此功能在React Native中不可用,如node-libs-react-native等第三方包

q3qa4bjr

q3qa4bjr2#

您可以使用此函数获取url参数,而无需任何库

export const getUrlParams = (url: string) => {
  const params: any = {};
  url.replace(/[?&]+([^=&]+)=([^&]*)/gi, (m, key, value) => {
    params[key] = value;
    return '';
  });
  return params;
};

相关问题