From 166e9a0e820646d67f5399b1b3acd6894083c1bb Mon Sep 17 00:00:00 2001 From: Netfan Date: Mon, 31 Mar 2025 11:51:57 +0800 Subject: [PATCH] chore: add demo for apiComponent with caching and concurrency (#5827) * chore: add demo for apiComponent with caching and concurrency * docs: update api component docs --- .../common-ui/vben-api-component.md | 11 ++++ .../vue-query/concurrency-caching.vue | 61 +++++++++++++++++++ .../views/demos/features/vue-query/index.vue | 17 +++++- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 playground/src/views/demos/features/vue-query/concurrency-caching.vue diff --git a/docs/src/components/common-ui/vben-api-component.md b/docs/src/components/common-ui/vben-api-component.md index caa9facf..de7fbb0a 100644 --- a/docs/src/components/common-ui/vben-api-component.md +++ b/docs/src/components/common-ui/vben-api-component.md @@ -123,6 +123,10 @@ function fetchApi(): Promise> { ::: +## 并发和缓存 + +有些场景下可能需要使用多个ApiComponent,它们使用了相同的远程数据源(例如用在可编辑的表格中)。如果直接将请求后端接口的函数传递给api属性,则每一个实例都会访问一次接口,这会造成资源浪费,是完全没有必要的。Tanstack Query提供了并发控制、缓存、重试等诸多特性,我们可以将接口请求函数用useQuery包装一下再传递给ApiComponent,这样的话无论页面有多少个使用相同数据源的ApiComponent实例,都只会发起一次远程请求。演示效果请参考 [Playground vue-query](http://localhost:5555/demos/features/vue-query),具体代码请查看项目文件[concurrency-caching](https://github.com/vbenjs/vue-vben-admin/blob/main/playground/src/views/demos/features/vue-query/concurrency-caching.vue) + ## API ### Props @@ -147,3 +151,10 @@ function fetchApi(): Promise> { | options | 直接传入选项数据,也作为api返回空数据时的后备数据 | `OptionsItem[]` | - | | visibleEvent | 触发重新请求数据的事件名 | `string` | - | | loadingSlot | 目标组件的插槽名称,用来显示一个"加载中"的图标 | `string` | - | + +### Methods + +| 方法 | 描述 | 类型 | 版本要求 | +| --- | --- | --- | --- | +| getComponentRef | 获取被包装的组件的实例 | ()=>T | >5.5.4 | +| updateParam | 设置接口请求参数(将与params属性合并) | (newParams: Record)=>void | >5.5.4 | diff --git a/playground/src/views/demos/features/vue-query/concurrency-caching.vue b/playground/src/views/demos/features/vue-query/concurrency-caching.vue new file mode 100644 index 00000000..1e0f3280 --- /dev/null +++ b/playground/src/views/demos/features/vue-query/concurrency-caching.vue @@ -0,0 +1,61 @@ + + diff --git a/playground/src/views/demos/features/vue-query/index.vue b/playground/src/views/demos/features/vue-query/index.vue index d57cf812..00dd6e4f 100644 --- a/playground/src/views/demos/features/vue-query/index.vue +++ b/playground/src/views/demos/features/vue-query/index.vue @@ -1,11 +1,15 @@