【发布时间】:2021-07-29 00:58:56
【问题描述】:
我需要从外部来源获取 IP 地址和端口号详细信息。这些详细信息是提出其他一些请求所必需的。以下是我正在尝试的代码:
import axios from 'axios'
let ServerURL
axios.get('http://some/path')
.then((response) => {
ServerURL = 'http://' + response.data.server_ip_address + ':' + response.data.server_ip_port
})
.catch((error) => {
console.log(error)
})
const apiClient = axios.create({
baseURL: ServerURL,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
})
export default {
getConfigInfo () {
return apiClient.get('/config')
}
}
我的问题是,在调用导出函数 getConfigInfo() 时,ServerURL 仍然未定义。
我该如何处理此类问题?非常感谢任何帮助。
【问题讨论】:
-
在第一个函数的
then中调用你的第二个函数 -
这里的问题是我需要在得到响应后导出一个函数。而且我无法在
then内导出。
标签: javascript ecmascript-6 promise axios