【问题标题】: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

【问题讨论】:

    标签: android ios reactjs react-native


    【解决方案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"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-02
        • 2017-01-03
        • 1970-01-01
        • 2022-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多