request为包装后的axios请求
1、直接return请求,得到promise,获取不到返回值,

 const tmp = async () => {
    return request({
      method: "post",
      url: "/auth/login",
      data: {
        username: name,
        password: pw,
      },
    });
    console.log("login request", tmp);

2、post请求使用params传递参数

const LoginApi = async (name: string, pw: string) => {
  const tmp = await request({
    method: "post",
    url: "/auth/login",
    params: {
      username: name,
      password: pw,
    },
  });
  console.log("login request", tmp);
};

3、没执行async函数

const LoginApi = (name: string, pw: string) => {
  async () => {
    const tmp = await request({
      method: "post",
      url: "/auth/login",
      data: {
        username: name,
        password: pw,
      },
    });
    console.log("login request", tmp);
  };
};
async () => {
        const res = await fetch(uri, { method: "GET" });
        const resText = await res.text();
        console.log("request:", resText);
      };

4、正确写法

const iGet= async () => {
        const res = await fetch(uri, { method: "GET" });
        const resText = await res.text();
        console.log("request:", resText);
      };
iGet()
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐