if (__DEV__) { const { emitsOptions, propsOptions: [propsOptions] } = instance if (emitsOptions) { if ( !(event in emitsOptions) && !( __COMPAT__ && (event.startsWith('hook:') || event.startsWith(compatModelEventPrefix)) ) ) { if (!propsOptions || !(toHandlerKey(event) in propsOptions)) { warn( `Component emitted event "${event}" but it is neither declared in ` + `the emits option nor as an "${toHandlerKey(event)}" prop.` ) } } else { const validator = emitsOptions[event] if (isFunction(validator)) { const isValid = validator(...rawArgs) if (!isValid) { warn( `Invalid event arguments: event validation failed for event "${event}".` ) } } } } }
let args = rawArgs // 查看事件是否是update:开头 const isModelListener = event.startsWith('update:')
// for v-model update:xxx events, apply modifiers on args // 是的话就截取掉,获得后面那段 const modelArg = isModelListener && event.slice(7) if (modelArg && modelArg in props) { const modifiersKey = `${ modelArg === 'modelValue' ? 'model' : modelArg }Modifiers` // 获取v-model的修饰符,回传的时候也会做处理 const { number, trim } = props[modifiersKey] || EMPTY_OBJ if (trim) { args = rawArgs.map(a => (isString(a) ? a.trim() : a)) } if (number) { args = rawArgs.map(looseToNumber) } }
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { devtoolsComponentEmit(instance, event, args) }
if (__DEV__) { const lowerCaseEvent = event.toLowerCase() if (lowerCaseEvent !== event && props[toHandlerKey(lowerCaseEvent)]) { warn( `Event "${lowerCaseEvent}" is emitted in component ` + `${formatComponentName( instance, instance.type )} but the handler is registered for "${event}". ` + `Note that HTML attributes are case-insensitive and you cannot use ` + `v-on to listen to camelCase events when using in-DOM templates. ` + `You should probably use "${hyphenate(event)}" instead of "${event}".` ) } }
// handlerName = on+事件名称 let handlerName let handler = props[(handlerName = toHandlerKey(event))] || // also try camelCase event handler (#2249) props[(handlerName = toHandlerKey(camelize(event)))] // for v-model update:xxx events, also trigger kebab-case equivalent // for props passed via kebab-case if (!handler && isModelListener) { handler = props[(handlerName = toHandlerKey(hyphenate(event)))] }