1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <template> <div> <p v-pre> 使用:value和@input</p> <input :value="modelValue" @input="inputEvent" /> <p v-pre>使用commputed属性</p> <input v-model="computedValue" /> <p v-pre> v-model:msg</p> <input v-model='computedMsg'/> </div> </template>
<script> export default { props: { modelValue: { type: String, default: '', required: true }, msg: { type: String, default: '', required: true }, modelModifiers: { default: () => ({}) }, msgModifiers: { default: () => ({}) } }, emits:['update:modelValue', 'update:msg'], methods: { inputEvent: function(event) { console.log(event.target.value); //原生操作 this.$emit('update:modelValue', event.target.value); } }, computed: { computedValue: { get: function() { return this.modelValue; }, set(val) { console.log(this.modelModifiers); // {mix: true} this.$emit('update:modelValue', val); } }, computedMsg: { get: function() { return this.msg; }, set(val) { console.log(this.msgModifiers); // {mm: true} this.$emit('update:msg', val); } } } } </script>
|