Javascript 如何修复';TypeError:undefined不是对象';进口期间
我正在尝试在React Native中组织我的公共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
主题
文件夹。但在尝试默认导出其他文件中的模块时发现问题。基本上,试图做到这一基本结构:
+-- 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中遇到过一些问题。谢谢