Просмотр исходного кода

fix(reactivity): should not observe frozen objects

fix #867
Evan You 6 лет назад
Родитель
Сommit
1b2149dbb2
2 измененных файлов с 9 добавлено и 1 удалено
  1. 7 0
      packages/reactivity/__tests__/reactive.spec.ts
  2. 2 1
      packages/reactivity/src/reactive.ts

+ 7 - 0
packages/reactivity/__tests__/reactive.spec.ts

@@ -155,6 +155,13 @@ describe('reactivity/reactive', () => {
     expect(isReactive(obj.bar)).toBe(false)
     expect(isReactive(obj.bar)).toBe(false)
   })
   })
 
 
+  test('should not observe frozen objects', () => {
+    const obj = reactive({
+      foo: Object.freeze({ a: 1 })
+    })
+    expect(isReactive(obj.foo)).toBe(false)
+  })
+
   describe('shallowReactive', () => {
   describe('shallowReactive', () => {
     test('should not make non-reactive properties reactive', () => {
     test('should not make non-reactive properties reactive', () => {
       const props = shallowReactive({ n: { foo: 1 } })
       const props = shallowReactive({ n: { foo: 1 } })

+ 2 - 1
packages/reactivity/src/reactive.ts

@@ -33,7 +33,8 @@ const canObserve = (value: any): boolean => {
     !value._isVue &&
     !value._isVue &&
     !value._isVNode &&
     !value._isVNode &&
     isObservableType(toRawType(value)) &&
     isObservableType(toRawType(value)) &&
-    !nonReactiveValues.has(value)
+    !nonReactiveValues.has(value) &&
+    !Object.isFrozen(value)
   )
   )
 }
 }