实现了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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM