导出代码组织的网址

时间:2017-06-28 11:59:20

标签: react-native

我对React本地人来说相当新,我希望将所有端点网址放在同一个地方

所以,我的environement.js:

module.exports = {
 SERVER: 'http://url.com',
 API_VERSION: '/api/v3',
 FULL_URL: this.SERVER + this.API_VERSION,
 PLAYERS:  this.FULL_URL + '/players',
 TEAMS:    this.FULL_URL + '/teams',
};

另一方面是getGame.js

GLOBAL = require('../environement');
function getGame() {
 console.log(GLOBAL.PLAYERS)
}

当我运行我的应用程序时,我得到了

undefined/players

我做错了什么?

由于

2 个答案:

答案 0 :(得分:0)

你可以尝试使用ES6这样的东西。

const SERVER = 'http://url.com';
const API_VERSION = '/api/v3';

export const FULL_URL = SERVER + API_VERSION;
export const PLAYERS = FULL_URL + '/players';
export const TEAMS = FULL_URL + '/teams';

然后

import { FULL_URL, PLAYERS, TEAMS } from 'path/to/the/file'

答案 1 :(得分:0)

请查看此解决方案:https://github.com/luggit/react-native-config

例如,当您在teamcity中拥有一些不同的环境并为app构建代理时,它将非常有用。

相关问题