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

fix prop default value observing when data() is also present (fix #1071 & #1072)

Evan You 11 лет назад
Родитель
Сommit
7b6913ede9
2 измененных файлов с 35 добавлено и 1 удалено
  1. 4 1
      src/instance/scope.js
  2. 31 0
      test/unit/specs/directives/prop_spec.js

+ 4 - 1
src/instance/scope.js

@@ -54,7 +54,10 @@ exports._initData = function () {
   if (optionsData) {
   if (optionsData) {
     this._data = optionsData
     this._data = optionsData
     for (var prop in propsData) {
     for (var prop in propsData) {
-      if (this._props[prop].raw !== null) {
+      if (
+        this._props[prop].raw !== null ||
+        !optionsData.hasOwnProperty(prop)
+      ) {
         optionsData.$set(prop, propsData[prop])
         optionsData.$set(prop, propsData[prop])
       }
       }
     }
     }

+ 31 - 0
test/unit/specs/directives/prop_spec.js

@@ -442,6 +442,37 @@ if (_.inBrowser) {
       })
       })
       expect(vm.$children[0].prop).toBe(true)
       expect(vm.$children[0].prop).toBe(true)
       expect(vm.$el.textContent).toBe('true')
       expect(vm.$el.textContent).toBe('true')
+      expect(JSON.stringify(vm.$children[0].$data)).toBe(JSON.stringify({
+        prop: true
+      }))
+    })
+
+    it('should initialize with default value when not provided & has default data', function () {
+      var vm = new Vue({
+        el: el,
+        template: '<test></test>',
+        components: {
+          test: {
+            props: {
+              prop: {
+                type: String,
+                default: 'hello'
+              }
+            },
+            data: function () {
+              return {
+                other: 'world'
+              }
+            },
+            template: '{{prop}} {{other}}'
+          }
+        }
+      })
+      expect(vm.$el.textContent).toBe('hello world')
+      expect(JSON.stringify(vm.$children[0].$data)).toBe(JSON.stringify({
+        other: 'world',
+        prop: 'hello'
+      }))
     })
     })
   })
   })
 }
 }