reactjs - 如何使用 swr 发送标头
问题描述
我正在尝试使用SWR和 Axios 发送标头,但没有发送标头。
const fetcher = (url) =>
axios
.get(url, { headers: { Authorization: "Bearer " + auth.token } })
.then((res) => res.data);
const { data, error } = useSWR(
`http://localhost:8000/api/v1/users/get-avatar`,
fetcher
);
if (error) console.log(error);
if (data) console.log(data);
使用 SWR 发送标头的正确方法是什么?
解决方案
根据文档https://swr.vercel.app/docs/arguments你应该做
const fetcher = (url, token) =>
axios
.get(url, { headers: { Authorization: "Bearer " + token } })
.then((res) => res.data);
const { data, error } = useSWR(
[`http://localhost:8000/api/v1/users/get-avatar`, auth.token],
fetcher
);
if (error) console.log(error);
if (data) console.log(data);
推荐阅读
- windows - 将 PowerShell foreach 循环转换为 /f 循环的批处理文件
- javascript - 如何在多个函数调用中保留相同的随机数?
- javascript - 函数节点 js 的数组别名
- lua - [{n,{}}] 在 lua 中做了什么?
- python - 禁用键盘但不断收到事件
- android - 当移动应用程序处于后台(iOS 和 Android)时,这个持续的通话中/进行中栏是什么?
- python - 有效的工作域给出“socket.gaierror: [Errno -2] Name or service not known”
- git - 更改 git 远程 url 以包含用户名而不丢失分支跟踪信息
- mailkit - winautomation V8 中的 MailKit 不接受阿拉伯字母
- c++ - Why does my display function terminate the program?