Pit*_*tty 1 export react-native
当我从另一个js文件访问一个js文件的const值时,获取的是未定义数据
Abc.js
import { Platform } from 'react-native';
const abc = Platform.select({
ios: [
'com.abc',
'com.xyz'
],
android: [
'com.pqr',
'com.lmn'
]
});
export default abc
Run Code Online (Sandbox Code Playgroud)
App.js
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
Button,
View
} from 'react-native';
import { abc } from "./Abc";
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
onPressgetData = () => {
console.warn("DATA " + JSON.stringify(abc));
}
export default class App extends Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
!! Welcome to React Native !!
</Text>
<Button title="get Data"
color="#841584"
onPress={() => onPressgetData()} />
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
Run Code Online (Sandbox Code Playgroud)
单击获取数据而不是数组时,我正在从App.js中访问abc const数据,但在警告中未定义。我不知道我在哪里弄错了谢谢
您已将模块导出为,default并将其导入为named import,因此未定义。
更改
import { abc } from "./Abc";
Run Code Online (Sandbox Code Playgroud)
至
import abc from "./Abc";
Run Code Online (Sandbox Code Playgroud)
得到正确的结果
当您从可以使用的文件中导出单个组件时export default abc,可以通过以下方式将其直接导入到其他文件中import abc from './abc'
当您的文件有许多组件/函数时,您必须使用单独导出每个函数export functionName并使用导入它们
import { functionName, functionName2, functionName3 } from './abc'
您还可以export default SpecialFunctionName与其他导出一起使用。所以,当你
import SpecialFunctionName(can use any name here) from './abc'
你将永远得到SpecialFunctionName原来的样子default export。
| 归档时间: |
|
| 查看次数: |
1165 次 |
| 最近记录: |