export function del (target: Array<any> | Object, key: any) {
// target 非法校验,只能为数组和对象这类引用类型添加新的索引值
if (process.env.NODE_ENV !== 'production' &&
(isUndef(target) || isPrimitive(target))
) {
warn(`Cannot delete reactive property on undefined, null, or primitive value: ${(target: any)}`)
}
// 如果是数组则使用 splice 来删除索引,因为 splice 已经被挟持过了,会触发对应依赖的响应
if (Array.isArray(target) && isValidArrayIndex(key)) {
target.splice(key, 1)
return
}
const ob = (target: any).__ob__
// 当 target 为 vue 实例,或者为根 root 时是不允许的,因为无法触发依赖响应
if (target._isVue || (ob && ob.vmCount)) {
process.env.NODE_ENV !== 'production' && warn(
'Avoid deleting properties on a Vue instance or its root $data ' +
'- just set it to null.'
)
return
}
// 如果本身就不存在响应的 key 值则直接结束
if (!hasOwn(target, key)) {
return
}
// 删除对应 key 值
delete target[key]
// 如果 target 并不是响应式的则直接结束
if (!ob) {
return
}
// 通知依赖响应
ob.dep.notify()
}