javascript - 箭头运算符不返回函数
问题描述
我是这个 es6 大会的新手,已经有几年没有接触 js 了。
什么有效
只有这个有效——
const apiClient = create({
baseURL: "http://" + IP + ":8990",
headers: { Authorization: "auth_token" },
});
上面的脚本是在getPosts.js中导入的:
import { apiClient } from "./client";
const endpoint = "/api/";
const getPosts = () => {
return apiClient.get(endpoint);
};
问题
但是,当我尝试在getPosts.js中导入以下任何内容时,它会说:
TypeError: _client.apiClient.get is not a function.
const apiClient = (auth_token="") => {
create({
baseURL: "http://" + IP + ":8990",
headers: { Authorization: auth_token },
});
};
const apiClient = () => {
return create({
baseURL: "http://" + IP + ":8990",
headers: { Authorization: "auth_token" },
});
};
const apiClient = () =>
create({
baseURL: "http://" + IP + ":8990",
headers: { Authorization: "auth_token" },
});
这是整个文件:
import { create } from "apisauce";
import IP from "../config/network";
const apiClient = create({
baseURL: "http://" + IP + ":8990",
headers: { Authorization: "auth_token" },
});
const setAuthToken = (token) => {
return console.log("in setAuthToken : " + token);
};
export { apiClient, setAuthToken };
我这样做是因为我想将参数传递给函数。谢谢。
解决方案
在第一种情况下,apiClient
设置为函数的返回值,该create
函数似乎是具有get
属性的对象,在其他情况下,apiClient
设置为返回返回值的函数create
。
你必须使用这样的东西:
apiClient().get(endpoint)
或者
apiClient(token).get(endpoint)
推荐阅读
- reactjs - React Material-UI not justify-content 不适用于我的所有卡片
- java - 如何将 MainActivity 中的值添加到 SecondActivity 中的列表
- c - 服务器仅向第一个客户端广播
- ffmpeg - 如何使用 ffmpeg hwaccel d3d11va 解码?
- reactjs - 为什么我的 React/JSX if 语句仍在执行?
- twig - 侧边栏区域内容显示在 drupal 8 的主要内容下方
- redis - 如何在 Apache ActiveMQ 中仅获取组中的最后一条消息?
- java - 通过循环从数组中删除多行
- encoding - HTML文件编码问题(中文)
- mysql - 混淆的MySQL答案加起来不一样