1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| function initData (vm) { var data = vm.$options.data; //获取到data的值,vm就是一个Component,在最开始的init赋予了this的值 data = vm._data = typeof data === 'function' //vm._data是一个私有变量,也可以通过this._data.xxx访问data的值 ? getData(data, vm) //判断data是否为一个函数,vue在定义组件的时候,data必须是一个函数 : data || {}; if (!isPlainObject(data)) { data = {}; warn( 'data functions should return an object:\n' + 'https://vuejs.org/v2/guide/components.html#data-Must-Be-a-Function', vm ); } // proxy data on instance var keys = Object.keys(data); var props = vm.$options.props; var methods = vm.$options.methods; var i = keys.length; while (i var key = keys[i]; { if (methods && hasOwn(methods, key)) {//函数名不能和data的值冲突 warn( ("Method \"" + key + "\" has already been defined as a data property."), vm ); } } if (props && hasOwn(props, key)) {//在props上定义的值不能再data定义,如果定义,会覆盖 warn( "The data property \"" + key + "\" is already declared as a prop. " + "Use prop default value instead.", vm ); } else if (!isReserved(key)) { proxy(vm, "_data", key); //proxy是代理 } } // observe data observe(data, true /* asRootData */); //监听data的值 }
|