在$emit 时,为什么要将类数组换成数组?哪里有类数组?难道不是vue 自己建的吗?
Vue.prototype.$emit = function (event: string): Component { // 这里的Component 就是 Vue 实例。 const vm: Component = this // 如果是生产环境,则不做任何处理 if (process.env.NODE_ENV !== 'production') { const lowerCaseEvent = event.toLowerCase() if (lowerCaseEvent !== event && vm._events[lowerCaseEvent]) { tip( `Event "${lowerCaseEvent}" is emitted in component ` + formatComponentName(vm) + ` 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}".` ) } } let cbs = vm._events[event] if (cbs) { // 将类数组的对象转换成数组 cbs = cbs.length > 1 ? toArray(cbs) : cbs const args = toArray(arguments, 1) // 遍历执行 for (let i = 0, l = cbs.length; i < l; i++) { cbs[i].apply(vm, args) } } return vm }
内容来源: answershuto/learnVue