我最近刚从 android 转到 React Native。所以需要一些帮助。为什么我无法访问同一类上的变量,例如,当我从另一个类调用 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;
关于javascript - React Native - 访问同一类上的静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460767/