/* * @Description: * @Author: Format-qi 283810417@qq.com * @Date: 2022-06-25 15:43:52 * @LastEditors: Format-qi 283810417@qq.com * @LastEditTime: 2022-06-25 15:45:39 */ import type { DescriptionProps, DescInstance, UseDescReturnType } from './typing'; import { ref, getCurrentInstance, unref } from 'vue'; export function useDescription(props?: Partial): UseDescReturnType { if (!getCurrentInstance()) { throw new Error('useDescription() can only be used inside setup() or functional components!'); } const desc = ref>(null); const loaded = ref(false); function register(instance: DescInstance) { if (unref(loaded)) { return; } desc.value = instance; props && instance.setDescProps(props); loaded.value = true; } const methods: DescInstance = { setDescProps: (descProps: Partial): void => { unref(desc)?.setDescProps(descProps); }, }; return [register, methods]; }