Explorar el Código

fix(core): Make set/delete warning condition for undefined, null and (#7818)

primitive values more precise.  Corrects #7452
Piotr Kaminski hace 8 años
padre
commit
9084747e30
Se han modificado 2 ficheros con 8 adiciones y 8 borrados
  1. 6 6
      src/core/observer/index.js
  2. 2 2
      test/unit/modules/observer/observer.spec.js

+ 6 - 6
src/core/observer/index.js

@@ -10,6 +10,8 @@ import {
   hasProto,
   isObject,
   isPlainObject,
+  isPrimitive,
+  isUndef,
   isValidArrayIndex,
   isServerRendering
 } from '../util/index'
@@ -195,10 +197,9 @@ export function defineReactive (
  */
 export function set (target: Array<any> | Object, key: any, val: any): any {
   if (process.env.NODE_ENV !== 'production' &&
-    !Array.isArray(target) &&
-    !isObject(target)
+    (isUndef(target) || isPrimitive(target))
   ) {
-    warn(`Cannot set reactive property on non-object/array value: ${target}`)
+    warn(`Cannot set reactive property on undefined, null, or primitive value: ${(target: any)}`)
   }
   if (Array.isArray(target) && isValidArrayIndex(key)) {
     target.length = Math.max(target.length, key)
@@ -231,10 +232,9 @@ export function set (target: Array<any> | Object, key: any, val: any): any {
  */
 export function del (target: Array<any> | Object, key: any) {
   if (process.env.NODE_ENV !== 'production' &&
-    !Array.isArray(target) &&
-    !isObject(target)
+    (isUndef(target) || isPrimitive(target))
   ) {
-    warn(`Cannot delete reactive property on non-object/array value: ${target}`)
+    warn(`Cannot delete reactive property on undefined, null, or primitive value: ${(target: any)}`)
   }
   if (Array.isArray(target) && isValidArrayIndex(key)) {
     target.splice(key, 1)

+ 2 - 2
test/unit/modules/observer/observer.spec.js

@@ -360,12 +360,12 @@ describe('Observer', () => {
     try {
       setProp(null, 'foo', 1)
     } catch (e) {}
-    expect(`Cannot set reactive property on non-object/array value`).toHaveBeenWarned()
+    expect(`Cannot set reactive property on undefined, null, or primitive value`).toHaveBeenWarned()
 
     try {
       delProp(null, 'foo')
     } catch (e) {}
-    expect(`Cannot delete reactive property on non-object/array value`).toHaveBeenWarned()
+    expect(`Cannot delete reactive property on undefined, null, or primitive value`).toHaveBeenWarned()
   })
 
   it('should lazy invoke existing getters', () => {