React Native - 在同一个 class 上访问静态变量
React Native - Accessing static variable on same class
我最近刚从 android 转移到 React Native。所以需要一些帮助。为什么我无法访问同一个 class 上的变量,例如当我从另一个 class 调用 URL_API_SERVER 时,它给我 'Undefined/api/v2'.
class Constant {
static BASE_URL = 'https://xxxxx';
static URL_API_SERVER = this.BASE_URL + '/api/v2';
static STATIC_BASEURL = this.BASE_URL + '/static';
static URLSTRING_FAQ = this.STATIC_BASEURL + '/FAQ.html';
static URLSTRING_TOU = this.STATIC_BASEURL + '/TOU.html';
}
export default Constant;
由于您正在使用 static
变量,因此您不能使用 this
。您可以像下面这样访问静态变量。
class Constant {
static BASE_URL = 'https://xxxxx';
static URL_API_SERVER = Constant.BASE_URL + '/api/v2';
static STATIC_BASEURL = Constant.BASE_URL + '/static';
static URLSTRING_FAQ = Constant.STATIC_BASEURL + '/FAQ.html';
static URLSTRING_TOU = Constant.STATIC_BASEURL + '/TOU.html';
}
export default Constant;
我最近刚从 android 转移到 React Native。所以需要一些帮助。为什么我无法访问同一个 class 上的变量,例如当我从另一个 class 调用 URL_API_SERVER 时,它给我 'Undefined/api/v2'.
class Constant {
static BASE_URL = 'https://xxxxx';
static URL_API_SERVER = this.BASE_URL + '/api/v2';
static STATIC_BASEURL = this.BASE_URL + '/static';
static URLSTRING_FAQ = this.STATIC_BASEURL + '/FAQ.html';
static URLSTRING_TOU = this.STATIC_BASEURL + '/TOU.html';
}
export default Constant;
由于您正在使用 static
变量,因此您不能使用 this
。您可以像下面这样访问静态变量。
class Constant {
static BASE_URL = 'https://xxxxx';
static URL_API_SERVER = Constant.BASE_URL + '/api/v2';
static STATIC_BASEURL = Constant.BASE_URL + '/static';
static URLSTRING_FAQ = Constant.STATIC_BASEURL + '/FAQ.html';
static URLSTRING_TOU = Constant.STATIC_BASEURL + '/TOU.html';
}
export default Constant;