實現了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