自定义vue组件使用v-model

<template>
    <div class="helect">
        <select v-model="val" @change="clickRadio">
            <option :value="v.value" v-for="(v,i) in List" :key="i">{{v.key}}</option>
        </select>
    </div>
</template>

<script>
    export default {
        props: {
            List: {
                type: Array,
                default: []
            },
            value: {
                type: String,
                default: ""
            }
        },
        data() {
            return {
                val: this.value
            }
        },
        model: {
            prop: "value", //绑定的值,通过父组件传递
            event: "update2" //自定义名 可以随便改
        },
        methods: {
            clickRadio() {
                this.$emit("update2", this.val); //子组件与父组件通讯,告知父组件更新绑定的值
            }
        },
    }
</script>

<style>

</style>
 <Hselect :List="urlList" v-model="url"></Hselect>

猜你喜欢

转载自blog.csdn.net/oYuLian/article/details/89498403