Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何修复';TypeError:undefined不是对象';进口期间_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 如何修复';TypeError:undefined不是对象';进口期间

Javascript 如何修复';TypeError:undefined不是对象';进口期间,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我正在尝试在React Native中组织我的公共主题文件夹。但在尝试默认导出其他文件中的模块时发现问题。基本上,试图做到这一基本结构: +-- components +-- theme | +-- appStyles.js | +-- colors.js | +-- index.js 在theme/appStyles.js中,将导出默认的单个对象(与colors.js相同): 在theme/index.js中,它将导出默认对象,映射主题文件夹中的文件: import AppStyles

我正在尝试在React Native中组织我的公共
主题
文件夹。但在尝试默认导出其他文件中的模块时发现问题。基本上,试图做到这一基本结构:

+-- components
+-- theme
|  +-- appStyles.js
|  +-- colors.js
|  +-- index.js
theme/appStyles.js
中,将导出默认的单个对象(与
colors.js
相同):

theme/index.js中,它将导出默认对象,映射主题文件夹中的文件:

import AppStyles from './appStyles';
import Colors from './colors';

export default {
  AppStyles,
  Colors,
};
当我试图在另一个模块中使用它时,即
SomeComponent.js
,它只是未定义且不可用:

import { AppStyles } from '../theme';

console.log(AppStyles); // would log 'undefined'

作为一项工作,我一直在做:

主题/appStyles.js
中:

export default {
  screen: {
    flex: 1,
  },
};
export const AppStyles = {
  screen: {
    flex: 1,
  },
};
export * from './appStyles';
import { AppStyles } from '../theme';

console.log(AppStyles); // would log Object structure
主题/index.js
中:

export default {
  screen: {
    flex: 1,
  },
};
export const AppStyles = {
  screen: {
    flex: 1,
  },
};
export * from './appStyles';
import { AppStyles } from '../theme';

console.log(AppStyles); // would log Object structure
然后它将在
SomeComponent.js中可用:

export default {
  screen: {
    flex: 1,
  },
};
export const AppStyles = {
  screen: {
    flex: 1,
  },
};
export * from './appStyles';
import { AppStyles } from '../theme';

console.log(AppStyles); // would log Object structure


在ReactJS Web中一直使用此“模式”。只是想知道React Native如何处理这个模块解析。

无需通过
{}

import AppStyles from './appStyles';

export default AppStyles;
而不是简单地用你想要的名字导入

import  AppStyles  from '../theme';

console.log(AppStyles);
我有多个东西要从索引中导出

import  AppStyles  from '../theme';
import  Color from '../color';
export{
  Appstyle as default,
  Color
}
而不是简单地作为

import  AppStyles, {Color}  from '../theme';

console.log(AppStyles);
console.log(Color);

主题文件夹中还有其他文件,如
colors.js
font.js
。然后将要从
index.js
文件导出为
{Colors,font,AppStyles}
@ademenial
。/theme
将始终指向
index.js
,如果要将所有导出的文件值合并到一个文件中,您可以在
索引中使用
命名导出
,然后使用
名称导入
明白了!重新阅读了文档。我认为这是react native特有的,因为我以前在bundler中遇到过一些问题。谢谢