官方地址: Vue.component — Vue.js
源码路径: src/core/instance/state.js initState方法 -> initData方法
源码如下(已删除不必要代码):
1.初始化数据
function initData (vm: Component) {
let data = vm.$options.data;
// 如果是函数 获取函数内的值data.call(vm, vm)
data = vm._data = typeof data === 'function'
? getData(data, vm)
: data || {}
// 确保 data props methods
// proxy data on instance
const keys = Object.keys(data)
const props = vm.$options.props
const methods = vm.$options.methods
let i = keys.length
while (i--) {
const key = keys[i]
if (process.env.NODE_ENV !== 'production') {
if (methods && hasOwn(methods, key)) {
warn(
`Method "${key}" has already been defined as a data property.`,
vm
)
}
}
if (props && hasOwn(props, key)) {
process.env.NODE_ENV !== 'production' && warn(
`The data property "${key}" is already declared as a prop. ` +
`Use prop default value instead.`,
vm
)
} else if (!isReserved(key)) { // // 检测字符串是否以 $ 或者 _ 开头
// 声明_data的代理 使得_data指向data
proxy(vm, `_data`, key)
}
}
// observe data
observe(data, true /* asRootData */)
}
2.观察value
/**
* Attempt to create an observer instance for a value,
* returns the new observer if successfully observed,
* or the existing observer if the value already has one.
*/
export function observe (value: any, asRootData: ?boolean): Observer | void {
if (!isObject(value) || value instanceof VNode) {
return
}
let ob: Observer | void
if (hasOwn(value, '__ob__') && value.__ob__ instanceof Observer) {
ob = value.__ob__
} else if ( // shouldObserve 当对象本身携带 __ob__ 属性时,shouldObserve 为 false。
shouldObserve &&
!isServerRendering() &&
(Array.isArray(value) || isPlainObject(value)) && // 数组或者对象
Object.isExtensible(value) && // 对象是否可扩展
!value._isVue // 避免观察 Vue 实例本身
) {
ob = new Observer(value)
}
if (asRootData && ob) {
ob.vmCount++
}
return ob
}
3.Observer处理value、数组循环、对象递归处理
详见
4.defineReactive
/**
* Define a reactive property on an Object.
*/
export function defineReactive (
obj: Object,
key: string,
val: any,
customSetter?: ?Function,
shallow?: boolean
) {
const dep = new Dep()
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptors
// 返回给定对象的所有自有属性描述符。
const property = Object.getOwnPropertyDescriptor(obj, key)
if (property && property.configurable === false) {
return
}
// cater for pre-defined getter/setters
const getter = property && property.get
const setter = property && property.set
if ((!getter || setter) && arguments.length === 2) {
val = obj[key]
}
// 子属性也绑定observe
let childOb = !shallow && observe(val)
// 给当前值绑定
Object.defineProperty(obj, key, {
enumerable: true,
configurable: true,
get: function reactiveGetter () {
const value = getter ? getter.call(obj) : val
// 依赖收集 vm触发get的时候 进行收集、收集当前的watcher
if (Dep.target) {
dep.depend()
if (childOb) {
// TODO
childOb.dep.depend()
if (Array.isArray(value)) {
dependArray(value)
}
}
}
return value
},
set: function reactiveSetter (newVal) {
const value = getter ? getter.call(obj) : val
/* eslint-disable no-self-compare */
if (newVal === value || (newVal !== newVal && value !== value)) {
return
}
/* eslint-enable no-self-compare */
if (process.env.NODE_ENV !== 'production' && customSetter) {
customSetter()
}
// #7981: for accessor properties without setter
if (getter && !setter) return
if (setter) {
setter.call(obj, newVal)
} else {
val = newVal
}
childOb = !shallow && observe(newVal)
dep.notify()
}
})
}
Dep.target 的作用重要。后续文章详解