.sync
修飾符update:my-prop-name
的模式觸發事件實現 上行綁定 最終實現 雙向綁定this.$emit('update:title', newTitle)
child.vuevue
<template> <div> <input type="text" v-model="sonValue"> <div>{{ fatherValue }}</div> </div> </template> <script> export default { props: { fatherValue: { required: true } }, data () { return { sonValue: this.fatherValue } }, watch: { sonValue (newValue, oldvalue) { this.$emit('update:fatherValue', newValue) }, fatherValue (newValue) { this.sonValue = newValue } } } </script>
father.vueui
<template> <div class="hello"> <!-- input實時改變value的值, 而且會實時改變child裏的內容 --> <input type="text" v-model="value"> <child :fatherValue.sync="value" ></child> </div> </template> <script> import Child from './Child' //引入Child子組件 export default { data() { return { value: '' } }, components: { 'child': Child } } </script>