你可以通过监听 props 中定义的值来更新组件值,监听组件内的值更新组件绑定的值稍微写了个二次封装组件的 demo 可以参考下 <template> <input type="text" v-model="currentValue" @change="change" /> </template> <script> export default { props: { value: { type: String } }, data() { return { currentValue: this.value } }, watch: { value(val) { this.currentValue = val }, currentValue(val) { if (this.value !== val) { this.$emit('input', val) } } }, methods: { change(e) { this.$emit('change', e) } } } </script>
你可以通过监听 props 中定义的值来更新组件值,监听组件内的值更新组件绑定的值
稍微写了个二次封装组件的 demo 可以参考下