【问题标题】:Create react native module and use it in application创建反应原生模块并在应用程序中使用它
【发布时间】:2019-03-06 13:18:55
【问题描述】:

我想为 iOS 和 Android 创建 react-native 模块并在应用程序中使用它。

我想像本地模块一样使用它,而不是上传到 npm。

当我按照官方文档中描述的步骤操作时,我总是在将模块导入 App.js 文件时遇到这些错误:

从 'react-test-module' 导入 MyModule;

找不到模块“react-test-module”的声明文件。 '/Users/dbellerose/Path/to/My/Module/react-test-module/index.js' 隐含了一个 'any' 类型。 如果存在,请尝试npm install @types/react-test-modulet,或者添加一个包含 `declare module 'react-test-module' 的新声明 (.d.ts) 文件;

当我运行 iOS 应用程序时:

无法从 /Users/Path/To/My/App/App.js 解析模块 react-test-module:模块 react-test-module 在 Haste 模块映射中不存在。

该消息给了我一些解决问题的提示,但它不起作用。

要解决,请尝试以下操作: 1.清除守望者手表:watchman watch-del-all。 2、删除node_modules文件夹:rm -rf node_modules && npm install。 3. 重置 Metro Bundler 缓存:rm -rf /tmp/metro-bundler-cache-*npm start -- --reset-cache。 4. 移除急速缓存:rm -rf /tmp/haste-map-react-native-packager-*.

那么你知道什么是创建模块并在应用程序中使用它而不将包上传到 npm 的好过程吗?

最好的问候,

大卫

【问题讨论】:

  • 你应该在你的模块文件中写下精确的方向
  • ^^ 正如 nerius 所说,您需要使用 ./some_path/module/component.ts 之类的目录链接到该文件 - 如果您想使用像 '@test_modules/test.module.ts` 这样的别名,那么告诉我,我会为你解答。
  • 您好,component.ts 文件是什么?我的模块中没有它

标签: ios reactjs react-native module


【解决方案1】:

以下是我可以作为清单与您分享的一些步骤:

  1. yarn global add create-react-native-module
  2. create-react-native-module MyLibrary
  3. CD 到 MyLibrary 文件夹,运行 yarn install
  4. 处理您的模块

要链接到您的项目,您需要将以下内容添加到您的 package.json 中(路径应该是您的模块所在的任何位置)

"react-native-test-module": "file:../", 
"react-native-my-library": "file:../../react-native-my-library/"

** 请记住,如果您在模块中附加了新方法。您需要:

  1. 从您的项目中移除 node_modules 和 Pods
  2. 重新执行yarn/npm install & pod install

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2020-04-06
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多