Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Need Help]v2的两个优化问题 #134

Closed
yuntian001 opened this issue Jun 13, 2022 · 10 comments
Closed

[Need Help]v2的两个优化问题 #134

yuntian001 opened this issue Jun 13, 2022 · 10 comments

Comments

@yuntian001
Copy link

v2的两个俩问题
1.index.d.ts 里面的 type Options 能不能导出 便于引用 二次封装自己请求函数。
2.缓存时现在是完全基于的cacheKey 进行缓存 我想基于cacheKey加请求参数去做缓存 应该怎么搞,用自定义的setCache 方法可以实现么,主要使用场景是 三级联动选择框组件,初始化时请求同一个接口,但是父级id不一样 我现在是manual设置为true 然后依次调用三次runAsync({id:praentId}),因为设置了是同一个封装request方法同一个cacheKey,结果拿到的数据从缓存取了。

Originally posted by @yuntian001 in #121 (comment)

@yuntian001 yuntian001 changed the title v2的两个优化俩问题 [Need Help]v2的两个优化问题 Jun 13, 2022
@John60676
Copy link
Member

1.index.d.ts 里面的 type Options 能不能导出 便于引用 二次封装自己请求函数。

下个版本会导出来

2.缓存时现在是完全基于的cacheKey 进行缓存 我想基于cacheKey加请求参数去做缓存 应该怎么搞,用自定义的setCache 方法可以实现么,主要使用场景是 三级联动选择框组件,初始化时请求同一个接口,但是父级id不一样 我现在是manual设置为true 然后依次调用三次runAsync({id:praentId}),因为设置了是同一个封装request方法同一个cacheKey,结果拿到的数据从缓存取了。

自定义缓存可以实现,你只要自己去管理 cacheKey 即可。但你仍需要显式的传入 cacheKey,即使你不是用它,因为这是开启缓存的依据。

const store = {};
const key = "some key";
const { runAsync } = useRequest(getUser, {
  cacheKey: key,
  manual: true,
  setCache: (_, d) => {
    store[key] = d;
  },
  getCache: () => {
    return store[key];
  },
});

@John60676 John60676 added the 2.x 2.x label Jun 14, 2022
@yuntian001
Copy link
Author

1.index.d.ts 里面的 type Options 能不能导出 便于引用 二次封装自己请求函数。

下个版本会导出来

2.缓存时现在是完全基于的cacheKey 进行缓存 我想基于cacheKey加请求参数去做缓存 应该怎么搞,用自定义的setCache 方法可以实现么,主要使用场景是 三级联动选择框组件,初始化时请求同一个接口,但是父级id不一样 我现在是manual设置为true 然后依次调用三次runAsync({id:praentId}),因为设置了是同一个封装request方法同一个cacheKey,结果拿到的数据从缓存取了。

自定义缓存可以实现,你只要自己去管理 cacheKey 即可。但你仍需要显式的传入 cacheKey,即使你不是用它,因为这是开启缓存的依据。

const store = {};
const key = "some key";
const { runAsync } = useRequest(getUser, {
  cacheKey: key,
  manual: true,
  setCache: (_, d) => {
    store[key] = d;
  },
  getCache: () => {
    return store[key];
  },
});

setCache 和 getCache 怎么才能拿到runAsync的入参。

@John60676
Copy link
Member

这需要 cacheKey 支持动态设置,目前还没实现,我看看吧

@yuntian001
Copy link
Author

这需要 cacheKey 支持动态设置,目前还没实现,我看看吧

好的,我临时在公共请求里自己封装了一层cache希望能迭代到v2版本中,非常感谢

John60676 added a commit that referenced this issue Jun 17, 2022
@John60676
Copy link
Member

rc 版本已支持

@John60676
Copy link
Member

这个问题我们已经在 v2 版本处理了,目前 v2 版本已经发布 rc 版本。

欢迎试用。

文档:https://next.attojs.com
源码:https://github.com/AttoJS/vue-request/tree/next

安装最新版本

$ npm install vue-request@next
# or
$ yarn add vue-request@next
# or
$ pnpm install vue-request@next

@yuntian001
Copy link
Author

yuntian001 commented Jun 30, 2022


<script>
import { useRequest } from "vue-request";
import axios from "axios";
import { onBeforeMount } from "vue";
export default {
  setup() {
    const getUser = (parentId) => {
      return axios.get("http://jsonplaceholder.typicode.com/posts",{params:{parentId}});
    };
    console.log(11)
    //请求组件所需初始化数据
    const { run,runAsync } = useRequest(getUser, {
      cacheKey: (params)=>{console.log('cacheKEY params',params);return params?params[0]:''},
      staleTime: 600000,
      manual :true
    });
    let api = async (params) => {
      console.log('run 被调用',params)
      let a = await runAsync(params);
    };
    api(1).then(api(2));

    return { api };
  },
};

// ...
</script>
<template>
  <div><button @click="api(1)">请求1</button></div>
  <div> <button @click="api(2)">请求2</button></div>
</template>


用上面的代码 点击请求1按钮 的时候,缓存有延迟依然请求接口了。
1656550542574

@John60676
Copy link
Member

https://codesandbox.io/s/muddy-fire-dn4y2y?file=/src/App.vue

没重现你说的问题

@yuntian001
Copy link
Author

yuntian001 commented Jun 30, 2022

你将网速设置为慢速3G 模拟弱网 连续点击按钮 可以出来
image

@John60676
Copy link
Member

这个问题之前会回复过了 #121 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants