This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo 资源

通用库,允许下载资源并将其与其他库一起使用。

Android
iOS
tvOS
Web
Included in Expo Go
Recommended version:
~58.0.13

expo-asset 提供了一个与 Expo 资源系统的接口。资源是指在应用源代码旁边存放的、在运行时应用需要的任何文件。示例包括图片、字体和音效。Expo 的资源系统与 React Native 的系统集成,因此你可以使用 require('path/to/file') 来引用文件。例如,这就是在 React Native 中如何引用静态图片文件用于 Image 组件的方式。更多信息请查看 React Native 的静态图片资源文档。这种引用静态图片资源的方法在 Expo 中可以开箱即用。

安装

🌐 Installation

Terminal
- npx expo install expo-asset
- yarn expo install expo-asset
- pnpm expo install expo-asset
- bun expo install expo-asset

If you are installing this in an existing React Native app, make sure to install expo in your project.

应用配置中的配置

🌐 Configuration in app config

如果你在项目中使用配置插件(连续原生生成 (CNG)),可以使用其内置的 配置插件 来配置 expo-asset。该插件允许你配置无法在运行时设置并且需要构建新的应用二进制文件才能生效的各种属性。如果你的应用不使用 CNG,那么你需要手动配置该库。

🌐 You can configure expo-asset using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-asset", { "assets": ["path/to/file.png", "path/to/directory"] } ] ] } }

Configurable properties

NameDefaultDescription
assets[]

An array of asset files or directories to link to the native project. The paths should be relative to the project root so that the file names, whether specified directly or using a directory, will become the resource names.

Supported file types:

  • Images: .png, .jpg, .gif
  • Media: .mp4, .mp3, .lottie, .riv
  • SQLite database files: .db
  • 3D Models: .glb

用法

🌐 Usage

了解更多关于如何使用 expo-asset 配置插件在你的项目中嵌入资源文件的信息,请访问 在构建时加载资源。

🌐 Learn more about how to use the expo-asset config plugin to embed an asset file in your project in Load an asset at build time.

应用接口

🌐 API

import { Asset } from 'expo-asset';

No API data file found, sorry!