父组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <template> <div> <Test v-model= "val" /> {{val}} </div> </template> <script> import Test from './Test.vue' export default { data(){ return { val: 'test value' } }, components:{ Test }, } </script> |
子组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <template> <input :value= "testVal" @input= "handleChange" /> </template> <script> export default { props:[ 'val' ], data(){ return { testVal: this .val } }, watch:{ val(propsVal){ this .testVal = propsVal } }, methods:{ handleChange(e){ this .testVal =e.target.value this .$emit( "input" , this .testVal) } }, } </script> |