Expo 模块 API:开始使用
了解 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 covers the latter.
使用 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.
-
使用生成的示例项目单独创建新模块 如果你想在多个项目中重复使用它或将其发布到 npm。
¥Create a new module in isolation with a generated example project if you want to reuse it in multiple projects or publish it to npm.
下一节将介绍这两个流程。
¥Both of these flows are covered in the next sections.
向现有应用添加新模块
¥Add a new module to an existing application
1
创建本地 Expo 模块
¥Create 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. This is the recommended way to create a local Expo module.
-
npx create-expo-module@latest --local
然后,如果你的项目没有生成原生项目(android 和 ios 目录),请运行以下命令,否则请跳过此命令:
¥Then, if your project doesn't have native projects generated (android and ios directories), run the following command, otherwise skip this command:
-
npx expo prebuild --clean
你可以在 CLI 提示符中提供有意义的模块名称。对于其余提示,你也可以接受默认建议。
¥You can provide a meaningful module name in the CLI prompt. For the rest of the prompts, you can also accept the default suggestions.
注意:如果你的项目根目录中有一个使用
npx expo prebuild
创建的预先存在的 ios 目录,则必须重新安装 pod:¥Note: If you have a pre-existing ios directory in your project's root which was created using
npx expo prebuild
, you must reinstall the pods:
-
npx pod-install
2
使用 EAS Update 而不使用 EAS Build
¥Use the local module
在你的应用中导入本地模块,例如在 App.js 或 App.tsx 或 app/index.tsx 中:
¥Import the local module in your application, for example in App.js or App.tsx or app/index.tsx:
%%placeholder-start%%...%%placeholder-end%%
import MyModule from './modules/my-module';
export default function App() {
return (
<View style={styles.container}>
<Text>{MyModule.hello()}</Text>
</View>
);
}
在你的终端中启动开发服务器,这样当你编辑原生模块并在下一步中构建应用时,更改将反映在应用中:
¥Start the development sever in your terminal so that when you edit the native module and build the app in the next step, the changes will be reflected in the app:
-
npx expo start
恭喜!你已经创建了一个本地 Expo 模块。你现在可以开始处理它。
¥Congratulations! You have created a local Expo module. You can now start working on it.
提示:你还可以使用绝对导入路径 通过应用这些配置更改。
3
编辑模块
¥Edit the module
要在本地开发和测试你的模块,让我们使用适用于 Android 和 iOS 的 Android Studio 和 Xcode。
¥To develop and test your module locally, let's use Android Studio and Xcode for Android and iOS.
安卓
¥Android
-
在 Android Studio 中从你的项目中打开 android 目录(由
npx expo prebuild
在步骤 1 中生成)。Gradle 可能需要一段时间才能完成同步原生目录项目。¥Open the android directory (that is generated by
npx expo prebuild
in step 1) from your project in Android Studio. It may take a while for Gradle to finish syncing the native directory project. -
项目同步后,打开 modules/my-module/android/src/main/java/expo/modules/mymodule/MyModule.kt 文件。
¥Once the project is synced, open modules/my-module/android/src/main/java/expo/modules/mymodule/MyModule.kt file.
-
将
hello
函数更改为返回不同的字符串,例如“Hello world!🌎🤖 并保存文件。¥Change
hello
function to return a different string, such as "Hello world! 🌎🤖" and save the file. -
通过单击顶部菜单栏中的“运行 'app'”按钮来构建应用,你将在屏幕上看到更改。
¥Build the app by clicking Run 'app' button from top menu bar and you will see the changes on the screen.
每次对原生代码进行更改时,你都必须重复构建步骤。
¥You have to repeat the build step anytime you make a change to the native code to the changes.
iOS 系统
¥iOS
-
通过运行
xed ios
命令在 Xcode 中从你的项目中打开 ios 目录(由npx expo prebuild
在步骤 1 中生成)。¥Open the ios directory (that is generated by
npx expo prebuild
in step 1) from your project in Xcode by runningxed ios
command. -
在 Pods > 开发 Pods > MyModule 下,打开 MyModule.swift 文件。
¥Under Pods > Development Pods > MyModule, open MyModule.swift file.
-
将
hello
函数更改为返回不同的字符串,例如“Hello world!🌎🍎 并保存文件。¥Change
hello
function to return a different string, such as "Hello world! 🌎🍎" and save the file. -
通过单击顶部菜单栏中的“运行”按钮或按 ⌘ Cmd + R 来构建你的应用,你将在屏幕上看到更改。
¥Build your app by clicking Run button from top menu bar or press ⌘ Cmd + R, you will see the changes on the screen.
每次对原生代码进行更改时,你都必须重复构建步骤。
¥You have to repeat the build step anytime you make a change to the native code to the changes.
提示:如果向模块添加新的原生文件或修改 expo-module.config.json,请使用npx pod-install
重新安装 pod。
注意:还有其他流程可以与你的应用并行处理 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.
使用示例项目创建新模块
¥Create a new module with an example project
1
创建 Expo 模块
¥Create 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@latest my-module
2
打开模块并启动开发服务器
¥Open the module and start the development server
导航到模块目录,然后通过运行以下命令打开 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
转到示例目录并在终端中启动开发服务器,这样当你编辑原生模块并在下一步中构建应用时,更改将反映在应用中:
¥Go to example directory and start the development sever in your terminal so that when you edit the native module and build the app in the next step, the changes will be reflected in the app:
-
cd example
-
npx expo start
注意:如果你使用的是 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
编辑模块
¥Edit the module
安卓
¥Android
-
打开 my-module/android/src/main/java/expo/modules/mymodule/MyModule.kt 文件。
¥Open my-module/android/src/main/java/expo/modules/mymodule/MyModule.kt file.
-
将
hello
函数更改为返回不同的字符串,例如“Hello world!🌎🤖 并保存文件。¥Change
hello
function to return a different string, such as "Hello world! 🌎🤖" and save the file. -
通过单击顶部菜单栏中的“运行 'app'”按钮来构建应用,你将在屏幕上看到更改。
¥Build the app by clicking Run 'app' button from top menu bar and you will see the changes on the screen.
每次对原生代码进行更改时,你都必须重复构建步骤。
¥You have to repeat the build step anytime you make a change to the native code to the changes.
iOS 系统
¥iOS
-
在 Pods > 开发 Pods > MyModule 下,打开 MyModule.swift 文件。
¥Under Pods > Development Pods > MyModule, open MyModule.swift file.
-
将
hello
函数更改为返回不同的字符串,例如“Hello world!🌎🍎 并保存文件。¥Change
hello
function to return a different string, such as "Hello world! 🌎🍎" and save the file. -
通过单击顶部菜单栏中的“运行”按钮或按 ⌘ Cmd + R 来构建你的应用,你将在屏幕上看到更改。
¥Build your app by clicking Run button from top menu bar or press ⌘ Cmd + R, you will see the changes on the screen.
每次对原生代码进行更改时,你都必须重复构建步骤。
¥You have to repeat the build step anytime you make a change to the native code to the changes.
提示:如果向模块添加新的原生文件或修改 expo-module.config.json,请使用npx pod-install
重新安装 pod。
下一步
¥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.
关于使用 Expo Modules API 创建保留设置的原生模块的教程。
关于使用 Swift 和 Kotlin 创建原生模块的参考。