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