forked from vuejs/composition-api
/
del.ts
38 lines (36 loc) · 964 Bytes
/
del.ts
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
import { AnyObject } from '../types/basic'
import { getVueConstructor } from '../runtimeContext'
import { hasOwn, isPrimitive, isUndef, isValidArrayIndex } from '../utils'
/**
* Delete a property and trigger change if necessary.
*/
export function del(target: AnyObject, key: any) {
const Vue = getVueConstructor()
const { warn } = Vue.util
if (__DEV__ && (isUndef(target) || isPrimitive(target))) {
warn(
`Cannot delete reactive property on undefined, null, or primitive value: ${target}`
)
}
if (Array.isArray(target) && isValidArrayIndex(key)) {
target.splice(key, 1)
return
}
const ob = target.__ob__
if (target._isVue || (ob && ob.vmCount)) {
__DEV__ &&
warn(
'Avoid deleting properties on a Vue instance or its root $data ' +
'- just set it to null.'
)
return
}
if (!hasOwn(target, key)) {
return
}
delete target[key]
if (!ob) {
return
}
ob.dep.notify()
}