在本教程中,学习如何创建新的 Expo 项目。
在本章中,我们将学习如何创建一个新的 Expo 项目以及如何运行它。
¥In this chapter, let's learn how to create a new Expo project and how to get it running.
¥Prerequisites
我们需要以下工具来开始:
¥We'll need the following tools to get started:
在物理设备上安装 Expo。
¥Install Expo Go on a physical device.
通过安装 系统要求 下列出的所需工具来准备开发。
¥Prepare for development by installing the required tools listed under system requirements.
本教程还假设你具有 JavaScript 和 React 的基本知识。如果你从未编写过 React 代码,请阅读 官方 React 教程。
¥This tutorial also assumes that you have a basic knowledge of JavaScript and React. If you have never written React code, go through the official React tutorial.
1
¥Initialize a new Expo app
我们将使用 create-expo-app
来初始化一个新的 Expo 应用。它是一个命令行工具,允许我们创建一个安装了 expo
包的新 React Native 项目。在终端中运行以下命令:
¥We will use create-expo-app
to initialize a new Expo app. It is a command line tool that allows us to create a new React Native project with the expo
package installed. Run the following command in your terminal:
# Create a project named StickerSmash
-
npx create-expo-app StickerSmash --template blank
# Navigate to the project directory
-
cd StickerSmash
此命令将为项目创建一个名为 StickerSmash 的新目录。create-expo-app
有一个 --template
选项,我们可以使用它来创建和设置具有不同预安装库的新项目。在本例中,我们使用 blank
,它安装了所需的最少库。我们将在本教程中继续根据需要添加更多库。
¥This command will create a new directory for the project with the name StickerSmash. The create-expo-app
has a --template
option, which we can use to create and set up a new project with different pre-installed libraries. In this case, we're using the blank
which installs the minimum required libraries. We'll continue to add more libraries throughout this tutorial when needed.
2
¥Download assets
我们将在本教程中使用这些资源。
下载存档后,解压并替换项目目录中现有的资源目录。这将覆盖新项目初始化时创建的默认资源。
¥After downloading the archive, unzip it and replace the existing assets directory with it in the project directory. This will override the default assets created when the new project was initialized.
现在,让我们在我们最喜欢的代码编辑器或 IDE 中打开项目目录。在本教程中,我们将使用 VS Code 作为示例。
¥Now, let's open the project directory in our favorite code editor or IDE. Throughout this tutorial, we will use VS Code for our examples.
3
¥Install dependencies
要在网络上运行该项目,我们需要安装以下依赖,这将有助于在网络上运行该项目:
¥To run the project on the web, we need to install the following dependencies that will help to run the project on the web:
-
npx expo install react-dom react-native-web @expo/metro-runtime
4
¥Run the app on mobile and web
在项目目录中,运行以下命令从终端启动 development server:
¥In the project directory, run the following command to start a development server from the terminal:
-
npx expo start
开发服务器运行后,启动应用的最简单方法是在物理设备上使用 Expo Go。欲了解更多信息,请参阅 Open app on a device。
¥Once the development server is running, the easiest way to launch the app is on a physical device with Expo Go. For more information, see Open app on a device.
要查看正在运行的 Web 应用,请在终端中按 w。它将在默认网络浏览器中打开网络应用。
¥To see the web app in action, press w in the terminal. It will open the web app in the default web browser.
一旦它在所有平台上运行,该项目应该如下所示:
¥Once it is running on all platforms, the project should look like this:
上面应用屏幕上显示的文本可以在项目目录根目录下的 App.js 文件中找到。它是项目的入口点,在开发服务器启动时执行。
¥The text displayed on the app's screen above can be found in the App.js file which is at the root of the project's directory. It is the entry point of the project and is executed when the development server starts.
¥Next step
我们已经创建了一个新的 Expo 项目,并准备开发我们的 StickerSmash 应用。
¥We have created a new Expo project and are ready to develop our StickerSmash app.
在下一章中,我们将学习如何构建应用的第一个屏幕。