1. v-model 绑定响应式数据 同vue2
let textVal = ref(1)
template
<input v-model="textVal" type="text"/>
2. <子组件 v-model:xxx = data> </子组件>,自组件触发改变data时的用法:
let textVal = ref(1)
setInterval(() => {
textVal.value += 1
}, 1000)
<Child v-model:parentModel=textVal v-model = textVal></Child>
const props = defineProps({
modelValue: {
default: 0,
require: true
},
parentModel: {
default: 0,
require: true
}
})
const emit = defineEmits(['update:modelValue', 'update:parentModel'])
const clickBtn = () => {
emit('update:modelValue', 0)
}
const clickBtn1 = () => {
emit('update:parentModel', 100)
}
Child组件:
<button @click="clickBtn">Child按钮: 归零 {{ modelValue }}</button>
<button @click="clickBtn1">Child按钮: 从100开始: {{ parentModel }}</button>