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

时间:2020-12-04 19:07:11   收藏:0   阅读:60

由于不能直接修改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>

 

原文:https://www.cnblogs.com/baixinL/p/14086869.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!