Vue's model attribute, two-way binding of custom components

Custom components do not have v-model intrinsic properties and corresponding intrinsic methods, so the model is used to set the properties of this relationship

The subcomponent code is as follows:


<-- 子组件 components -->
export default {
    model:{
        prop:'parameter',// v-model接收的值 = props的parameter属性接收的值
        event:'fun'//v-model发生变化时触发的方法
    },
    props: {
        parameter: ''
    },
    ...
    methods:{
        fun(val){
            console.log(val)
        }
    }
}

The use of the parent component is as follows:


<template>
    <-- 父组件中设置keys,子组件修改对应值并触发函数 -->
    <my-components v-model="keys"></my-components>
</template>

import myComponents from ‘./components’

export default {
    cpmponents:{
        myComponents
    },
    data() {
        return {
            keys:'',
        }
    }
}

Guess you like

Origin blog.csdn.net/Samuel_OY/article/details/128827194