import { Ref, ref, reactive, toRaw } from 'vue'; import { $parent } from '../libs/function/$parent' export interface IParentData { modelValue: string[] | null max: number | null disabled: boolean | null disabledLabel: boolean | null shape: string | null activeColor: string | null size: number | null width: string | null wrap: string | null iconSize: number | null } /** * 获取父组件的数据 */ export const useGetParentData = (parentName = '', parent: Ref = ref(null)) => { // 避免再created中定义parent变量 if (!parent.value) parent.value = null const parentData = reactive({ value: null, max: null, disabled: null, disabledLabel: null, shape: null, activeColor: null, size: null, width: null, wrap: null, iconSize: null }) // 通过获取父组件实例 // 将父组件this中对应的参数,赋值给本组件的parentData对象中对应的属性 // 头条小程序不支持通过this.parent.xxx去监听父组件参数的变化,所以需要本方法进行实现 parent.value = $parent(parentName) if (parent.value) { // 遍历parentData中的属性,将parent中同名的属性赋值给parentData Object.keys(toRaw(parentData)).map(key => { parentData[key] = parent.value[key] }) } console.log(parent, parentData) return { parent, parentData } }