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