javascript - React Native - 访问同一类上的静态变量

标签 javascript reactjs react-native native

我最近刚从 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/

相关文章:

javascript substr 奇怪的行为?

javascript - 我不断收到的控制台警告是什么 - 延迟长时间运行的计时器任务以提高滚动平滑度?

javascript - 为参数的条件编写 If 语句

javascript - react-select 2 设置值/标签

javascript - ReactJS 不显示 backgroundImage 内联 CSS

json - 如何获取 json 嵌套对象?

android - iBeacon协议(protocol)如何监控20多个区域?

JavaScript 正则表达式 : why match number with trailing decimal?

reactjs - 在每个时间间隔更改文本 - React

带有 React Native 的 iOS 自适应布局