Skip to content

Vben ApiComponent

ApiComponent is a wrapper used to attach remote-option loading behavior to an existing component while preserving the original component usage pattern.

Common Usage

The current wrapper flow is:

  • pass the target component through component
  • fetch remote data through api
  • transform data through beforeFetch and afterFetch
  • map remote fields through resultField, valueField, labelField, and childrenField
  • pass normalized options to the target component through optionsPropName
vue
<script lang="ts" setup>
import { ApiComponent } from '@vben/common-ui';

import { Cascader } from 'ant-design-vue';

function fetchApi() {
  return Promise.resolve([
    {
      label: 'Zhejiang',
      value: 'zhejiang',
      children: [{ label: 'Hangzhou', value: 'hangzhou' }],
    },
  ]);
}
</script>

<template>
  <ApiComponent
    :api="fetchApi"
    :component="Cascader"
    :immediate="false"
    children-field="children"
    loading-slot="suffixIcon"
    visible-event="onDropdownVisibleChange"
  />
</template>

Current Props

PropDescriptionType
componentwrapped target componentComponent
apiremote request function(arg?: any) => Promise<any>
paramsextra request paramsRecord<string, any>
beforeFetchhook before requestAnyPromiseFunction
afterFetchhook after requestAnyPromiseFunction
visibleEventevent name used to lazy-load datastring
loadingSlotslot name used to render the loading iconstring
modelPropNamemodel prop name of the wrapped componentstring
autoSelectauto-pick the first / last / only option, or use a custom function'first' | 'last' | 'one' | ((items) => item) | false

Exposed Methods

MethodDescription
getComponentRef()returns the wrapped component instance
updateParam(newParams)merges and updates request params
getOptions()returns loaded options
getValue()returns the current bound value

Contributors

The avatar of contributor named as xingyu4j xingyu4j

Changelog

Released under the MIT License.