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

New*_*009 5 javascript native reactjs react-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;
Run Code Online (Sandbox Code Playgroud)

Jic*_*son 7

由于您使用的是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;
Run Code Online (Sandbox Code Playgroud)