import { createVNode, ref, render, VNode } from 'vue' import { ElButton } from 'element-plus' import StepLoadingItem from './components/step-loading-item' interface IOption { request: () => Promise name: string } export default function (options: Array = []) { let vm: VNode = null const parent = document.body const LoadingComponent = { name: 'SkStepLoading', setup() { const arr: Array<'success' | 'error' | 'loading'> = new Array(options.length).fill('loading') const statusArr = ref(arr) function destroySelf (e) { if (e.target === e.currentTarget && statusArr.value.includes('error') || e === '') { if (vm.el && vm.el.parentNode) { vm.el.parentNode.removeChild(vm.el) } } } async function run () { for (const [index, item] of options.entries()) { try { const res = await item.request() if (res) statusArr.value[index] = 'success' else{ statusArr.value.fill('error', index, options.length) return } } catch (e) { statusArr.value.fill('error', index, options.length) return } } destroySelf('') } const reRun = () => { statusArr.value = new Array(options.length).fill('loading') run() } run() return () => (
{destroySelf(e)}}>
{ options.map((item, index) => { return ( ) as any }) } { statusArr.value.includes('error') && // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore 重试 }
) }, } vm = createVNode(LoadingComponent) render(vm, document.createElement('div')) parent.appendChild(vm.el as HTMLElement) }