以编程方式读取app.json(或exp.json)

cse*_*lus 10 react-native expo

有没有办法app.json从您的应用程序中以编程方式读取内容,因此您可以获取当前版本号并在" 关于"屏幕中显示它?

bre*_*tne 30

您可以通过Expo.Constants.manifest访问它 - 这包括您的app.json配置,没有任何潜在的敏感信息,如API密钥.

import { Constants } from 'expo'; // 'expo-constants' with the latest SDK to date
Constants.manifest.version // your app version
Run Code Online (Sandbox Code Playgroud)

  • 这在 SDK 46 中不起作用。 (3认同)

Rah*_*ari 6

Constants.manifest 现在已弃用,您可以通过 Constants.expoConfig 访问它。这包括您的 app.json 配置,没有任何潜在的敏感信息,例如 API 密钥。

import Constants from 'expo-constants';
Constants.expoConfig.version
Run Code Online (Sandbox Code Playgroud)


小智 5

对于 Expo SDK 35,我这样做了:

expo install expo-constants
Run Code Online (Sandbox Code Playgroud)

在你的 .js 中:

import Constants from "expo-constants";

<Text> {Constants.manifest.description} </Text>
Run Code Online (Sandbox Code Playgroud)