了解 Expo 模块 API 入门。
有两种方法可以开始使用 Expo Modules API:你可以从头开始初始化新模块,也可以将 Expo Modules API 添加到现有模块。本指南将引导你从头开始创建新模块,集成到现有的库指南中 涵盖了后一种情况。
¥There are two ways to get started with the Expo Modules API: you can either initialize a new module from scratch or add the Expo Modules API to an existing module. This guide will walk you through creating a new module from scratch, and the Integrating in an existing library guide covers the latter case.
使用 Expo Modules API 创建新模块的两个推荐流程:
¥The two recommended flows to create a new module with Expo Modules API:
将新模块添加到现有的 Expo 应用,并使用它来测试和开发你的模块。
¥Add a new module to an existing Expo application, and use it to test and develop your module.
下一节将介绍这两个流程。
¥Both of these flows are covered in the next sections.
¥Adding a new module to an existing application
1
¥Creating the local Expo module
导航到你的项目目录(包含 package.json 文件的目录)并运行以下命令,这是创建本地 Expo 模块的推荐方法:
¥Navigate to your project directory (the one that contains the package.json file) and run the following command, which is the recommended way to create a local Expo module:
-
npx create-expo-module@latest --local
最好提供一个有意义的模块名称。但是,你可以接受其余问题的默认建议。
¥It's best to provide a meaningful module name. However, you can accept the default suggestions for the rest of the questions.
2
¥Using the module
如果你的项目中有使用 npx expo prebuild
创建的 ios 目录,则必须重新安装 pod:
¥If you have an ios directory in your project that you created using npx expo prebuild
, you must reinstall the pods:
-
pod install --project-directory=ios
注意:如果你使用的是开发客户端,则每次想要使用新的原生代码时都需要重新构建开发客户端。
¥Note: If you're using a development client, you need to rebuild your development client any time you want to use new native code.
现在,将模块导入到你的应用中,例如 App.js 或 App.tsx 中:
¥Now, import the module in your application, for example in App.js or App.tsx:
import { hello } from './modules/my-module';
要在本地运行你的应用,请运行 prebuild
命令,然后编译该应用:
¥To run your app locally, run the prebuild
command and then compile the app:
# Re-generate the native project directories from scratch
-
npx expo prebuild --clean
# Run the example app on Android
-
npx expo run:android
# Run the example app on iOS
-
npx expo run:ios
恭喜!你已经创建了一个本地 Expo 模块。你现在可以开始处理它。
¥Congratulations! You have created a local Expo module. You can now start working on it.
注意:你还可以使用绝对导入路径 进行一些配置更改。
¥Note: You can also use absolute import paths with some configuration changes.
3
¥Editing the module
¥Android
如果你的项目中有使用 npx expo prebuild
创建的 android 目录,你可以在 Android Studio 中打开该目录。
¥If you have an android directory in your project that you created using npx expo prebuild
, you can open the directory in Android Studio.
你始终可以直接在你喜欢的文本编辑器中编辑 modules/my-module/android/src/main/java/expo/modules/mymodule/
目录中的文件。
¥You can always just edit the files in the modules/my-module/android/src/main/java/expo/modules/mymodule/
directory directly in your favorite text editor.
更改 hello
方法以返回不同的字符串。例如,你可以将其更改为返回“Hello world!🌎🤖 .
¥Change the hello
method to return a different string. For example, you can change it to return "Hello world! 🌎🤖".
重新构建应用或构建新的开发客户端,你应该会看到你的更改。
¥Rebuild the app or build a new development client and you should see your change.
¥iOS
在你喜欢的代码编辑器中打开 module/my-module/ios/ 目录中的文件进行编辑。或者,如果你的项目中有一个使用 npx expo prebuild
创建的 ios 目录,你可以使用 Xcode 来编辑它们。
¥Open the files in modules/my-module/ios/ directory in your favorite code editor to edit them. Alternatively, if you have an ios directory in your project that was created using npx expo prebuild
, you can use Xcode to edit them.
现在,更改 hello
方法以返回不同的字符串。例如,你可以将其更改为返回“Hello world!🌎🍎 .
¥Now, change the hello
method to return a different string. For example, you can change it to return "Hello world! 🌎🍎".
重新构建应用或构建新的开发客户端,你应该会看到你的更改。请记住,每次进行原生更改时都需要运行 npx expo prebuild
,或者使用 pod install --project-directory="example/ios"
重新安装 Pod(这应该更快)。
¥Rebuild the app or build a new development client and you should see your change. Remember you need to either run npx expo prebuild
each time you make a native change or reinstall the pods using pod install --project-directory="example/ios"
(which should be way faster).
注意:还有其他流程可以与你的应用并行处理 Expo 模块。例如,你可以使用 monorepo 或发布到 npm,如 如何使用独立的 Expo 模块 指南中所述。
¥Note: There are also other flows for working on an Expo module in parallel with your application. For example, you can use a monorepo or publish to npm, as described in the How to use a standalone Expo module guide.
¥Creating a new module with an example project
1
¥Creating the Expo module
要从头开始创建新的 Expo 模块,请运行 create-expo-module
脚本,如下所示。该脚本将询问你几个问题,然后生成原生 Expo 模块以及使用你的新模块的 Android 和 iOS 示例应用。
¥To create a new Expo module from scratch, run the create-expo-module
script as shown below.
The script will ask you a few questions and then generate the native Expo module along with the example app for Android and iOS that uses your new module.
-
npx create-expo-module my-module
2
¥Running the example project
导航到模块目录,然后通过运行以下命令打开 Android 和/或 iOS 示例项目:
¥Navigate to the module directory and then open the Android and/or iOS example project by running the following commands:
-
cd my-module
-
npm run open:android
-
npm run open:ios
现在,你可以在你的设备或模拟器/模拟器上运行示例项目。当项目编译并运行时,你将看到“Hello world!👋 屏幕上。
¥Now, you can run the example project on your device or simulator/emulator. When the project compiles and runs, you will see "Hello world! 👋" on the screen.
注意:如果你使用的是 Windows,则可以通过在 Android Studio 中打开 android 目录来打开示例项目,但无法打开 iOS 项目文件。
¥Note: If you're using Windows, you can open the example project by opening the android directory in Android Studio, but you cannot open the iOS project files.
3
¥Making a change
¥Android
在 Android Studio 中打开 MyModuleModule.kt(⌘ Cmd + O 或 Ctrl + O 并搜索 MyModuleModule.kt)。更改 hello
方法以返回不同的字符串。例如,你可以将其更改为返回 "Hello world! 🌎🤖"
。重建应用,你应该会看到更改。
¥Open up MyModuleModule.kt in Android Studio (⌘ Cmd + O or Ctrl + O and search for MyModuleModule.kt). Change the hello
method to return a different string. For example, you can change it to return "Hello world! 🌎🤖"
. Rebuild the app and you should see your change.
¥iOS
在 Xcode 中打开 MyModuleModule.swift(⌘ Cmd + O 或 Ctrl + O 并搜索 MyModuleModule.swift)。更改 hello
方法以返回不同的字符串。例如,你可以将其更改为返回 "Hello world! 🌎🍎"
。
¥Open up MyModuleModule.swift in Xcode (⌘ Cmd + O or Ctrl + O and search for MyModuleModule.swift). Change the hello
method to return a different string. For example, you can change it to return "Hello world! 🌎🍎"
.
如果添加了新的原生文件,则需要使用 pod install --project-directory="example/ios"
重新安装 pod。
¥If you added new native files, you need to reinstall the pods using pod install --project-directory="example/ios"
.
重建应用,你应该会看到更改。
¥Rebuild the app and you should see your change.
¥Next steps
现在你已经了解了如何初始化模块并对其进行简单的更改,你可以继续学习教程或直接深入了解 API 参考。
¥Now that you have learned how to initialize a module and make simple changes to it, you can continue to a tutorial or dive right into the API reference.
创建一个简单但完整的原生模块来与 Android 和 iOS 首选项 API 进行交互。
Expo Module API 的概述以及常见模式(例如将事件从原生代码发送到 JavaScript)。