如何在 React Native 中使用通过别名安装的 npm 包

问题描述 投票:0回答:1

我有一个 React Native 应用程序,我需要为其实现外部键盘导航支持。我正在尝试安装第三方软件包('react-native-a11y')来帮助解决此问题。但是,我已经有一个名为react-native-a11y的本地包,重命名/重新配置将需要大量工作,因此我尝试使用别名('react-native-a11y-helper' ).

我的 package.json 中有类似以下内容:

 "react-native-a11y-alias": "npm:[email protected]",

pnpm install 后,该包将显示在 node_modules 中,并以所需的别名作为包名称。然后在我的代码中,我从包中导入一个组件,在构建时没有任何问题:

import { KeyboardFocusView } from 'react-native-a11y-alias';

但是当我在 Android 设备上运行该应用程序时,metro 捆绑程序会抛出以下错误:

error: Error: Unable to resolve module react-native-a11y-helper from <file>.tsx: react-native-a11y-helper could not be found within the project or in these directories:
  node_modules
  ../node_modules

我也尝试过使用

require
导入,但没有成功。

如何在RN中正确使用这个包?

react-native accessibility npm-install
1个回答
0
投票

react-native-a11y
的文件:

这个库尚未完成,目前处于测试阶段。

所以你需要仔细遵循安装公会

  1. npm i react-native-a11y
  2. cd ios && pod install
  3. 添加到
    MainActivity.java
  //android/app/src/main/java/com/project-name/MainActivity.java

  ...
  import android.content.Intent;
  import android.content.res.Configuration;
  ...
  
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Intent intent = new Intent("onConfigurationChanged");
    intent.putExtra("newConfig", newConfig);
    this.sendBroadcast(intent);
  }
  1. 仅限 iOS 将键盘(游戏)二进制文件与库链接:
  • 打开xcode
  • 在项目栏中选择文件夹
  • 选择目标项目
  • 选择构建阶段
  • 用库扩展链接二进制文件
  • 按加号图标
  • 您可以搜索游戏
  • 选择GameController.framework、GameKit.framework、GameplayKit.framework
  1. 将提供程序添加到应用程序的根目录:
watch: examples/A11ySample/App.tsx

export const App = () => {
  return (
    <A11yProvider>
        // content here
    </A11yProvider>
  );
};
© www.soinside.com 2019 - 2024. All rights reserved.