实现了input与变量的数据双向绑定

由于不能直接修改props的传递的变量,故使用$emit

<template>
    <view>
        <input ref='input1' type="text" placeholder="请输入" @input="inputChange" />
    </view>
</template>

<script>
    export default {
        props: ['transVal'],
        data() {
            return {
            };
        },
        watch: {
            transVal(val) {
                (this.$refs.input1.valueSync!= val) && (this.$refs.input1.valueSync = val)
            }
        },
        methods: {
            inputChange(e) {
                this.$emit('changeVal', e.detail.value)
            }
        }
    }
</script>

<style>

</style>

调用:<private-input :transVal="inputVal" @changeVal="inputVal=$event"></private-input>

 

posted @ 2020-12-04 17:17  baixinL  阅读(2374)  评论(0编辑  收藏  举报