Skip to content

Commit 93c1a50

Browse files
committed
do not warn instantiation data that overlap with props
1 parent 8dd598c commit 93c1a50

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/instance/internal/init.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ export default function (Vue) {
8181
this.$parent.$children.push(this)
8282
}
8383

84+
// save raw constructor data before merge
85+
// so that we know which properties are provided at
86+
// instantiation.
87+
if (process.env.NODE_ENV !== 'production') {
88+
this._runtimeData = options.data
89+
}
90+
8491
// merge options.
8592
options = this.$options = mergeOptions(
8693
this.constructor.options,

src/instance/internal/state.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,19 @@ export default function (Vue) {
8080
var propsData = this._data
8181
var optionsDataFn = this.$options.data
8282
var optionsData = optionsDataFn && optionsDataFn()
83+
var runtimeData
84+
if (process.env.NODE_ENV !== 'production') {
85+
runtimeData = (typeof this._runtimeData === 'function'
86+
? this._runtimeData()
87+
: this._runtimeData) || {}
88+
this._runtimeData = null
89+
}
8390
if (optionsData) {
8491
this._data = optionsData
8592
for (var prop in propsData) {
8693
if (process.env.NODE_ENV !== 'production' &&
87-
hasOwn(optionsData, prop)) {
94+
hasOwn(optionsData, prop) &&
95+
!hasOwn(runtimeData, prop)) {
8896
warn(
8997
'Data field "' + prop + '" is already defined ' +
9098
'as a prop. Use prop default value instead.'

test/unit/specs/directives/internal/prop_spec.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,16 +562,36 @@ describe('prop', function () {
562562
})
563563

564564
it('should warn data fields already defined as a prop', function () {
565+
var Comp = Vue.extend({
566+
data: function () {
567+
return { a: 123 }
568+
},
569+
props: {
570+
a: null
571+
}
572+
})
565573
new Vue({
574+
el: el,
575+
template: '<comp a="1"></comp>',
576+
components: {
577+
comp: Comp
578+
}
579+
})
580+
expect(hasWarned('already defined as a prop')).toBe(true)
581+
})
582+
583+
it('should not warn data fields already defined as a prop if it is from instantiation call', function () {
584+
var vm = new Vue({
566585
el: el,
567586
props: {
568587
a: null
569588
},
570589
data: {
571-
a: 1
590+
a: 123
572591
}
573592
})
574-
expect(hasWarned('already defined as a prop')).toBe(true)
593+
expect(getWarnCount()).toBe(0)
594+
expect(vm.a).toBe(123)
575595
})
576596

577597
it('should not warn for non-required, absent prop', function () {

0 commit comments

Comments
 (0)