Skip to content

Commit 7b6913e

Browse files
committed
fix prop default value observing when data() is also present (fix vuejs#1071 & vuejs#1072)
1 parent eb95383 commit 7b6913e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/instance/scope.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ exports._initData = function () {
5454
if (optionsData) {
5555
this._data = optionsData
5656
for (var prop in propsData) {
57-
if (this._props[prop].raw !== null) {
57+
if (
58+
this._props[prop].raw !== null ||
59+
!optionsData.hasOwnProperty(prop)
60+
) {
5861
optionsData.$set(prop, propsData[prop])
5962
}
6063
}

test/unit/specs/directives/prop_spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,37 @@ if (_.inBrowser) {
442442
})
443443
expect(vm.$children[0].prop).toBe(true)
444444
expect(vm.$el.textContent).toBe('true')
445+
expect(JSON.stringify(vm.$children[0].$data)).toBe(JSON.stringify({
446+
prop: true
447+
}))
448+
})
449+
450+
it('should initialize with default value when not provided & has default data', function () {
451+
var vm = new Vue({
452+
el: el,
453+
template: '<test></test>',
454+
components: {
455+
test: {
456+
props: {
457+
prop: {
458+
type: String,
459+
default: 'hello'
460+
}
461+
},
462+
data: function () {
463+
return {
464+
other: 'world'
465+
}
466+
},
467+
template: '{{prop}} {{other}}'
468+
}
469+
}
470+
})
471+
expect(vm.$el.textContent).toBe('hello world')
472+
expect(JSON.stringify(vm.$children[0].$data)).toBe(JSON.stringify({
473+
other: 'world',
474+
prop: 'hello'
475+
}))
445476
})
446477
})
447478
}

0 commit comments

Comments
 (0)