1.Vue父子组件通信方式
父子组件通信方式一般为props和emit组合使用,那么在不同的文件中应该如何使用呢?
|.vue文件和.jsx文件中有什么不同吗?
2. 不同文件间的通信方式
- 父组件vue文件和子组件vue文件
<HelloWorld :value="count" @update:value="handleAppValue" />
const props = defineProps<{ value: number }>();
const emit = defineEmits<{
(e: "update:value", value: number): void;
}>();
const handleUpdate = () => {
emit("update:value", props.value + 1);
};
</script>
<template>
<div @click="handleUpdate">{{ value }}</div>
</template>
2.父组件jsx文件和子组件vue文件
const handleUpdateValue = (count: number) => {
value.value = count;
emit("update:value", value.value);
};
return () => (
<HelloWorld value={value.value} onUpdate:value={handleUpdateValue} />
);
const props = defineProps<{ value: number }>();
const emit = defineEmits<{
(e: "update:value", value: number): void;
}>();
const handleUpdate = () => {
emit("update:value", props.value + 1);
};
</script>
<template>
<div @click="handleUpdate">{{ value }}</div>
</template>
3.父组件vue文件和子组件jsx文件
const count = ref(2);
const handleAppValue = (value: number) => {
count.value = value;
};
<TsxTest :value="count" @update:value="handleAppValue" />
props: {
value: {
type: Number,
default: 1,
},
},
emits: ["update:value"],
setup(props, { emit }) {
const handleUpdateValue = () => {
emit("update:value", props.value + 1);
};
return () => (
<div onClick={handleUpdateValue}>{props.value}</div>
);
},
3.父组件jsx文件和子组件jsx文件
const value = ref(1);
const handleUpdateValue = (count: number) => {
value.value = count;
};
<TsxTest value={value.value} onUpdate:value={handleUpdateValue} />
props: {
value: {
type: Number,
default: 1,
},
},
emits: ["update:value"],
setup(props, { emit }) {
const handleUpdateValue = () => {
emit("update:value", props.value + 1);
};
return () => (
<div onClick={handleUpdateValue}>{props.value}</div>
);
},
3.如何实现
在componentEmits文件里面可以看到
let args = rawArgs
const isModelListener = event.startsWith('update:')
const modelArg = isModelListener && event.slice(7)
if (modelArg && modelArg in props) {
const modifiersKey = `${
modelArg === 'modelValue' ? 'model' : modelArg
}Modifiers`
const { number, trim } = props[modifiersKey] || EMPTY_OBJ
if (trim) {
args = rawArgs.map(a => a.trim())
}
if (number) {
args = rawArgs.map(toNumber)
}
}
为啥modifiersKey会拼接Modifiers字符串呢? 因为在vModel处理时会获取父组件传过来的modifiers并进行处理拼接
const eventName = arg
? isStaticExp(arg)
? `onUpdate:${arg.content}`
: createCompoundExpression(['"onUpdate:" + ', arg])
: `onUpdate:modelValue`
const modifiersKey = arg
? isStaticExp(arg)
? `${arg.content}Modifiers`
: createCompoundExpression([arg, ' + "Modifiers"'])
: `modelModifiers`
然后在进行handler处理
let handlerName
let handler =
props[(handlerName = toHandlerKey(event))] ||
props[(handlerName = toHandlerKey(camelize(event)))]
if (!handler && isModelListener) {
handler = props[(handlerName = toHandlerKey(hyphenate(event)))]
}
if (handler) {
callWithAsyncErrorHandling(
handler,
instance,
ErrorCodes.COMPONENT_EVENT_HANDLER,
args
)
}
简单来说emit函数就是语法糖
<TsxTest value={value.value} onUpdate:value={handleUpdateValue} />
<TsxTest :value="count" @update:value="handleAppValue" />
上面两种方式的处理函数[onUpdate:value/@update:value]都会在emit里面变成这样
emit('update:value', count)
props.['onUpdate:value'](count)
|