UI コンポーネントの二次カプセル化は、元のコンポーネントのすべてのプロパティ、イベント、スロットを継承します。

UI コンポーネントの二次カプセル化は、元のコンポーネントのすべてのプロパティ、イベント、スロットを継承します。

使用属性:
$attrs和$listeners

親コンポーネント

<template>
    <a-input v-bind="$attrs" v-on="$listeners" ref="ipt">
        <template v-for="(value,name) in $slots" #[name]="slotData">
            <slot :name="name" v-bind="slotData"></slot>
        </template>
    </a-input>
</template>
<script>

export default {
    
    
    props: {
    
    
        icon: {
    
    
            type: String, // 可以自己定义属性,做其他处理
            default: ''
        }
    },

    mounted() {
    
    
        // 1.属性
        // console.log(this.$attrs)
        // 2.插槽
        // console.log(this.$slots)
        // 3.作用域插槽
        // <slot :name="name" v-bind="slotData"></slot>
        // 4.事件
         for (let key in this.$listeners) {
    
    
            this.$refs.ipt[key] = e => {
    
    
                 this.$emit(key, e)
             }
         }
        // 5.实例
        // console.log(this.$refs.ipt)
        let refsObj = Object.entries(this.$refs.ipt)
        refsObj.forEach(([key, value]) => {
    
    
            if (typeof value === 'function') {
    
    
                 this[key] = value
            }
         });
    }

}
</script>

サブアセンブリ

 <MyInput ref="ipt" :maxLength="5" v-model="222" placeholder="input with clear icon" icon="eye"
        @onChange="changeHandle" >
        <a-icon slot="prefix" type="user" />
 </MyInput>

おすすめ

転載: blog.csdn.net/qq_61950936/article/details/131697290