如何在另一个文件中使用该变量? (反应原生)

Posted

技术标签:

【中文标题】如何在另一个文件中使用该变量? (反应原生)【英文标题】:How can I use the variable here in another file? (React-Native) 【发布时间】:2021-04-07 11:30:44 【问题描述】:

如何在另一个文件中使用 'const a' 变量?

import React,  Component  from 'react';
import  View, Text, StyleSheet, TouchableOpacity, Animated, Dimensions  from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import colorsdata from '../../assets/data/colorsdata';

const  width, height  = Dimensions.get("window");

const a = colorsdata[0].word

【问题讨论】:

【参考方案1】:

导出它。像这样:

import React,  Component  from 'react';
import  View, Text, StyleSheet, TouchableOpacity, Animated, Dimensions  from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';
import colorsdata from '../../assets/data/colorsdata';

const  width, height  = Dimensions.get("window");

const a = colorsdata[0].word

export   a ;

然后导入它。像这样:

import  a  from "./Path_or_FileName"

【讨论】:

【参考方案2】:

你可以像这样导出一个变量

export const a = colorsdata[0].word

使其在其文件之外可用。然后,您可以通过使用文件的路径导入它来在另一个文件中使用它:

import  a  from "./path-to-file"

【讨论】:

以上是关于如何在另一个文件中使用该变量? (反应原生)的主要内容,如果未能解决你的问题,请参考以下文章