Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript/React中从另一个文件调用全局字符串_Javascript_Variables_Reactjs_Global - Fatal编程技术网

在Javascript/React中从另一个文件调用全局字符串

在Javascript/React中从另一个文件调用全局字符串,javascript,variables,reactjs,global,Javascript,Variables,Reactjs,Global,我有一个获取API URL的全局变量,我想将它添加到action.js文件中 mainUrl.js const MAIN_URL="www.testexamplebeta.com" action.js import {MAIN_URL} from '../mainUrl' export function fetchPets() { return function(dispatch) { console.log("THE URL IS", MAIN_URL)

我有一个获取API URL的全局变量,我想将它添加到action.js文件中

mainUrl.js

const MAIN_URL="www.testexamplebeta.com"
action.js

import {MAIN_URL} from '../mainUrl'

export function fetchPets() {
    return function(dispatch) {
        console.log("THE URL IS", MAIN_URL)
        axios.get(`${MAIN_URL}/tests`)
            .then(response => {
                dispatch({
                    type: FETCH_TESTS,
                    payload: response
                });
            })
            .catch(() => {
                console.log("Can't fetch the test examples");
            });
    }
}
当I console.log主URL未定义时。

您应该像这样导出“全局”变量:

export const MAIN_URL="www.testexamplebeta.com"
您应该像这样导出“全局”变量:

export const MAIN_URL="www.testexamplebeta.com"