zoukankan      html  css  js  c++  java
  • vue.js 源代码学习笔记 ----- instance event

    /* @flow */
    
    import { updateListeners } from '../vdom/helpers/index'
    import { toArray, tip, hyphenate, formatComponentName } from '../util/index'
    
    export function initEvents (vm: Component) {
      vm._events = Object.create(null)
      vm._hasHookEvent = false
      // init parent attached events 把父级的监听加载到vm中
      const listeners = vm.$options._parentListeners
      if (listeners) {
        updateComponentListeners(vm, listeners)
      }
    }
    
    let target: Component
    
    //绑定事件
    function add (event, fn, once) { if (once) { target.$once(event, fn) } else { target.$on(event, fn) } } function remove (event, fn) { target.$off(event, fn) } export function updateComponentListeners ( vm: Component, listeners: Object, oldListeners: ?Object ) { target = vm updateListeners(listeners, oldListeners || {}, add, remove, vm) } export function eventsMixin (Vue: Class<Component>) { const hookRE = /^hook:/ Vue.prototype.$on = function (event: string | Array<string>, fn: Function): Component { const vm: Component = this if (Array.isArray(event)) { for (let i = 0, l = event.length; i < l; i++) { this.$on(event[i], fn) } } else { (vm._events[event] || (vm._events[event] = [])).push(fn) // optimize hook:event cost by using a boolean flag marked at registration
        // 优化事件成本 通过布尔值
    // instead of a hash lookup if (hookRE.test(event)) { vm._hasHookEvent = true } } return vm } Vue.prototype.$once = function (event: string, fn: Function): Component { const vm: Component = this function on () { vm.$off(event, on) fn.apply(vm, arguments) } on.fn = fn vm.$on(event, on) return vm } Vue.prototype.$off = function (event?: string | Array<string>, fn?: Function): Component { const vm: Component = this // all if (!arguments.length) { vm._events = Object.create(null) return vm } // array of events if (Array.isArray(event)) { for (let i = 0, l = event.length; i < l; i++) { this.$off(event[i], fn) } return vm } // specific event const cbs = vm._events[event] if (!cbs) { return vm } if (arguments.length === 1) { vm._events[event] = null return vm } // specific handler 在 vm._events 中移除事件 let cb let i = cbs.length while (i--) { cb = cbs[i] if (cb === fn || cb.fn === fn) { cbs.splice(i, 1) break } } return vm } Vue.prototype.$emit = function (event: string): Component { 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}". ` + //注意 html属性 是大小写铭感的, 你不能利用 v-on 来监听驼峰写法的事件, 在模板中
          `Note that HTML attributes are
    case-insensitive and you cannot use ` + `v-on to listen to camelCase events when using in-DOM templates. ` +
    //你应该使用 show-one 来替代 showOne `You should probably use "${hyphenate(event)}" instead of "${event}".` ) } } let cbs = vm._events[event]
      //执行events
    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 } }
  • 相关阅读:
    app专项测试之稳定性测试-monkey测试
    Mac下Fiddler的安装启动。
    测试环境搭建和部署(在Linux环境下搭建jdk+Tomcat+mysql环境和项目包的部署)
    mysql如何用sql语句修改表字段?
    VMware虚拟机出现“正在使用中”如何解决?
    Opencv2.1+vs2008生成不依赖编译环境的exe文件
    VS2008在win7下不时出现Microsoft Incremental Linker已停止工作
    没有找到opencv_core231d.dll运行结果不能显示
    Mat类型与IplImage类型之间相互转换
    获取当前可执行文件的路径(绝对路径)
  • 原文地址:https://www.cnblogs.com/dhsz/p/7116420.html
Copyright © 2011-2022 走看看