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

Calling a Global String from another File in Javascript/React

我有一个获取 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");
            });
    }
}

当我 console.log MAIN_URL 我变得不确定。

您应该像这样导出 "global" 变量:

export const MAIN_URL="www.testexamplebeta.com"