(vm, methods)
| 5137 | |
| 5138 | //初始化事件Methods 把事件 冒泡到 vm[key] 虚拟dom 最外层中 |
| 5139 | function initMethods(vm, methods) { |
| 5140 | var props = vm.$options.props; |
| 5141 | //循环 methods 事件对象 |
| 5142 | for (var key in methods) { |
| 5143 | { |
| 5144 | //如果事件是null则发出警告 |
| 5145 | if (methods[key] == null) { |
| 5146 | warn( |
| 5147 | "Method \"" + key + "\" has an undefined value in the component definition. " + |
| 5148 | "Did you reference the function correctly?", |
| 5149 | vm |
| 5150 | ); |
| 5151 | } |
| 5152 | |
| 5153 | //判断key是否是改对象实例化的 |
| 5154 | //如果属性中定义了key,则在methods中不能定义同样的key |
| 5155 | if (props && hasOwn(props, key)) { |
| 5156 | warn( |
| 5157 | ("Method \"" + key + "\" has already been defined as a prop."), |
| 5158 | vm |
| 5159 | ); |
| 5160 | } |
| 5161 | //isReserved 检查一个字符串是否以$或者_开头的字母 |
| 5162 | if ((key in vm) && isReserved(key)) { //事件不能以$或者_开头的字母 |
| 5163 | warn( |
| 5164 | "Method \"" + key + "\" conflicts with an existing Vue instance method. " + |
| 5165 | "Avoid defining component methods that start with _ or $." |
| 5166 | ); |
| 5167 | } |
| 5168 | } |
| 5169 | //把事件放在最外层对象中,如果是函数为空则给一个空函数,如果是有函数则执行改函数 |
| 5170 | vm[key] = methods[key] == null ? noop : bind(methods[key], vm); |
| 5171 | } |
| 5172 | } |
| 5173 | |
| 5174 | //初始化Watch监听 |
| 5175 | function initWatch(vm, watch) { |
no test coverage detected