Vue 中 v-model 在自定义组件中的使用

<div id="app">
    "inputValue 的值:" + {{inputValue}}
    <my-input v-model="inputValue"></my-input>
</div>
Vue.component('my-input', {
    template: '<div><input type="text" ref="el" :value="value" @input="onInput"/></div>',
    props: {
        value: String
    },
    methods: {
        onInput() {
            this.$emit('input', this.$refs.el.value);
        }
    }
});
new Vue({
    el: '#app',
    data: {
        inputValue: '10'
    }
});
posted @ 2020-07-02 15:36  月半流云  阅读(380)  评论(0编辑  收藏  举报