Appearance
Vue
深层子组件使用上层组件的方法
方式一:使用 project inject
ts
provide('make', function (){
console.dir('make');
});ts
const make = inject('make')
make();方式二:使用第三方包 mitt
ts
import _ from 'mitt';
const mitt = _();
export default mitt;ts
import mitt from "./mitt";
mitt.on('make', function () {
console.dir('make')
})ts
import mitt from "./mitt";
mitt.emit('make');v-model object
html
<script setup>
const character = reactive({
pet: {
name: 'foo'
}
})
</script>
<template>
<pet v-model="character.pet"></pet>
</template>html
<script setup lang="ts">
interface Pet{
name: string
}
interface Props{
modelValue: Pet
}
const props = defineProps<Pet>()
const emit = defineEmits(['update:modelValue']);
const _ = props.modelValue || {name: 'bar'}
const pet = reactive(_)
watch(pet, () => {
emit('update:modelValue', pet);
})
</script>
<template>
<input v-model="pet.name" />
</template>