1、defineProps类型
defineProps<{ msg: string }>()
defineProps({
msg: {
type: String,
default: "1",
},
});
withDefaults(defineProps<{ msg: string }>(), {
msg: "1",
});
2、defineEmits类型
const emit = defineEmits<{
(e: "change", id: number): void;
}>();
function add() {
emit("change", count.value);
}
3、defineModel类型
<HelloWorld msg="Vite + Vue" v-model:count="count" />
const countV = defineModel("count", { type: Number, required: true, default: 1 });
function uodate() {
countV.value = 2;
}
4、父组件里面调用子组件方法
function add() {
countV.value = 2;
}
defineExpose({ add });
const ChildRef = ref<InstanceType<typeof HelloWorld> | null>(null);
function add() {
ChildRef.value?.add();
}