一.uni-app
<components v-model='value' />
<script>
export default {
model: {
prop: 'value',
event: 'input'
},
props: {
value: {
type: String,
default: ''
}
},
data() {
return: {
textareaVal: ''
}
},
watch: {
value(val) { this.textareaVal = val }
},
methods: {
onInput() {
this.$emit('input', this.val)
}
}
}
</script>
二.vue2
<components v-model='value' />
<script>
export default {
model: {
prop: 'perModelSelectVisible',
event: 'getPerModelSelectVisible'
},
props: {
perModelSelectVisible: {
type: Boolean,
default: false
}
},
data() {
return: {
visible: false
}
},
watch: {
value(val) { this.visible= val }
},
methods: {
onInput() {
this.visible = false
this.$emit('input', this.visible)
}
}
}
</script>
二.vue3
<components v-model:modelValue='visible' />
<template>
<a-modal
v-model:visible="visible"
title="批量选择"
@cancel="onCancel"
>
<p>Some contents...</p>
<p>Some contents...</p>
<p>Some contents...</p>
</a-modal>
</template>
<script setup lang="ts">
import { ref, watch } from 'vue'
const props = defineProps({
modelValue: {
type: Boolean
}
})
const emit = defineEmits([ 'update:modelValue' ])
const visible = ref<boolean>(false)
const onCancel = () => {
visible.value = false
console.log(visible.value)
emit('update:modelValue', visible.value)
}
watch(() => props.modelValue,(newVal,oldVal) => {
visible.value = newVal
},{immediate:true})
</script>
<style scoped>
</style>