未定义不是对象(UIConstants)

时间:2018-07-30 10:23:09

标签: javascript reactjs react-native

在react-native中,我有这个课程(来自kitchenTricks):

  import {Platform} from 'react-native';

export class UIConstants {
  static AppbarHeight = Platform.OS === 'ios' ? 44 : 56;
  static StatusbarHeight = Platform.OS === 'ios' ? 20 : 0;
  static HeaderHeight = UIConstants.AppbarHeight + UIConstants.StatusbarHeight;
}

类的名称:AppConstants.js

问题是当我尝试导出此类时。我收到此错误:

undefined is not an object(evaluating 'UIConstants.AppbarHeight')

2 个答案:

答案 0 :(得分:2)

在声明时不能引用同一个类。尝试更早定义高度:

const appbarHeight = Platform.OS === 'ios' ? 44 : 56;
const statusbarHeight = Platform.OS === 'ios' ? 20 : 0;

export class UIConstants {
  static AppbarHeight = appbarHeight;
  static StatusbarHeight = statusbarHeight;
  static HeaderHeight = appbarHeight + statusbarHeight;
}

答案 1 :(得分:0)

在使用UI常量的地方,应该像导入

import {UIConstants} from './UIConstants'

不是

import UIConstants from './UIConstants'