- 7.3 initMethods
7.3 initMethods
initMethod方法和这一节介绍的响应式没有任何的关系,他的实现也相对简单,主要是保证methods方法定义必须是函数,且命名不能和props重复,最终会将定义的方法都挂载到根实例上。
function initMethods (vm, methods) {var props = vm.$options.props;for (var key in methods) {{// method必须为函数形式if (typeof methods[key] !== 'function') {warn("Method \"" + key + "\" has type \"" + (typeof methods[key]) + "\" in the component definition. " +"Did you reference the function correctly?",vm);}// methods方法名不能和props重复if (props && hasOwn(props, key)) {warn(("Method \"" + key + "\" has already been defined as a prop."),vm);}// 不能以_ or $.这些Vue保留标志开头if ((key in vm) && isReserved(key)) {warn("Method \"" + key + "\" conflicts with an existing Vue instance method. " +"Avoid defining component methods that start with _ or $.");}}// 直接挂载到实例的属性上,可以通过vm[method]访问。vm[key] = typeof methods[key] !== 'function' ? noop : bind(methods[key], vm);}}
