Browse Source

polish: raise warning when Vue.set/delete is called on invalid values (#7452)

close #7381
dev
İsmail Arılık 7 years ago
committed by Evan You
parent
commit
e9ea565d91
  1. 12
      src/core/observer/index.js
  2. 12
      test/unit/modules/observer/observer.spec.js

12
src/core/observer/index.js

@ -194,6 +194,12 @@ export function defineReactive (
* already exist.
*/
export function set (target: Array<any> | Object, key: any, val: any): any {
if (process.env.NODE_ENV !== 'production' &&
!Array.isArray(target) &&
!isObject(target)
) {
warn(`Cannot set reactive property on non-object/array value: ${target}`)
}
if (Array.isArray(target) && isValidArrayIndex(key)) {
target.length = Math.max(target.length, key)
target.splice(key, 1, val)
@ -224,6 +230,12 @@ export function set (target: Array<any> | Object, key: any, val: any): any {
* Delete a property and trigger change if necessary.
*/
export function del (target: Array<any> | Object, key: any) {
if (process.env.NODE_ENV !== 'production' &&
!Array.isArray(target) &&
!isObject(target)
) {
warn(`Cannot delete reactive property on non-object/array value: ${target}`)
}
if (Array.isArray(target) && isValidArrayIndex(key)) {
target.splice(key, 1)
return

12
test/unit/modules/observer/observer.spec.js

@ -356,6 +356,18 @@ describe('Observer', () => {
})
})
it('warn set/delete on non valid values', () => {
try {
setProp(null, 'foo', 1)
} catch (e) {}
expect(`Cannot set reactive property on non-object/array value`).toHaveBeenWarned()
try {
delProp(null, 'foo')
} catch (e) {}
expect(`Cannot delete reactive property on non-object/array value`).toHaveBeenWarned()
})
it('should lazy invoke existing getters', () => {
const obj = {}
let called = false

Loading…
Cancel
Save