gpt4 book ai didi

javascript - 如何将 URL 从 API 传递到 Action (ReactJS)

转载 作者:行者123 更新时间:2023-12-03 05:11:20 24 4
gpt4 key购买 nike

获取API文件

import { apiUrl } from '../config'
const baseUri = apiUrl
const uri = {
outline : '/course/income/outline'
}
const getURI = (key) => baseUri + uri[key]

module.exports = { apiMiddleware, get, post, put, ...{ delete: del }, uri, getURI }

尝试将其传递到我的 axios URL

import Api from '../middleware/api'
export function IncomeList () {

return dispatch => {

return (

axios.post(Api.getURI(outline),{}, {
headers: { 'X-Authenticated-Userid': '15000500000@1' }
}).then(function (response) {


console.log(response.data);
dispatch(receiveData(response.data.body));

})
.catch((error) => {
console.log(error);
})
)
}
}

但是我收到错误 Uncaught ReferenceError: 轮廓未定义。如何传递正确的URL?

最佳答案

将字符串文字传递给 getURI 方法:

Api.getURI('outline')

调用 Api.getURI(outline) 会使解释器查找 outline 变量,该变量在当前作用域中未定义(因此出现 ReferenceError)。

专业提示:linter,例如 ESLint ,会尽早发现这个错误。

关于javascript - 如何将 URL 从 API 传递到 Action (ReactJS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41805971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com