functionVue$3 (options) { if ("development" !== 'production' && !(thisinstanceof Vue$3)) { warn('Vue is a constructor and should be called with the `new` keyword'); } this._init(options); }
functioninitMixin (Vue) { Vue.prototype._init = function (options) { var vm = this; // 略 initLifecycle(vm); initEvents(vm); callHook(vm, 'beforeCreate'); initState(vm); callHook(vm, 'created'); initRender(vm); }; }
因此我們直接來看 initState 的函式,行數不多就全部貼上來了:
1 2 3 4 5 6 7 8 9 10 11 12 13
functioninitState (vm) { vm._watchers = []; var opts = vm.$options; if (opts.props) { initProps(vm, opts.props); } if (opts.methods) { initMethods(vm, opts.methods); } if (opts.data) { initData(vm); } else { observe(vm._data = {}, true/* asRootData */); } if (opts.computed) { initComputed(vm, opts.computed); } if (opts.watch) { initWatch(vm, opts.watch); } }
這邊可以看到檢查property 的階段為 props -> methods -> data -> computed -> watch 比照我們剛剛收到的warning,property "message" will be overwritten by a computed property ,data初始化階段比computed 還早,當然會被改寫。
var data = vm.$options.data; data = vm._data = typeof data === 'function' ? data.call(vm) : data || {}; if (!isPlainObject(data)) { data = {}; "development" !== 'production' && 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 i = keys.length; while (i--) { if (props && hasOwn(props, keys[i])) { "development" !== 'production' && warn( "The data property \"" + (keys[i]) + "\" is already declared as a prop. " + "Use prop default value instead.", vm ); } else { proxy(vm, keys[i]); } } // observe data observe(data, true/* asRootData */); }