Skip to content

Commit 9f6c23f

Browse files
committed
add "propsData" option
1 parent a0624f4 commit 9f6c23f

File tree

5 files changed

+11
-19
lines changed

5 files changed

+11
-19
lines changed

src/compiler/compile-props.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ function makePropsLinkFn (props) {
163163
return function propsLinkFn (vm, scope) {
164164
// store resolved props info
165165
vm._props = {}
166+
var inlineProps = vm.$options.propsData
166167
var i = props.length
167168
var prop, path, options, value, raw
168169
while (i--) {
@@ -171,7 +172,9 @@ function makePropsLinkFn (props) {
171172
path = prop.path
172173
options = prop.options
173174
vm._props[path] = prop
174-
if (raw === null) {
175+
if (inlineProps && hasOwn(inlineProps, path)) {
176+
initProp(vm, prop, inlineProps[path])
177+
} if (raw === null) {
175178
// initialize absent prop
176179
initProp(vm, prop, undefined)
177180
} else if (prop.dynamic) {

src/instance/internal/state.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,14 @@ export default function (Vue) {
9797
// 1. it's not already defined as a prop
9898
// 2. it's provided via a instantiation option AND there are no
9999
// template prop present
100-
if (
101-
!props || !hasOwn(props, key) ||
102-
(data && hasOwn(data, key) && props[key].raw === null)
103-
) {
100+
if (!props || !hasOwn(props, key)) {
104101
this._proxy(key)
105102
} else if (process.env.NODE_ENV !== 'production') {
106103
warn(
107104
'Data field "' + key + '" is already defined ' +
108-
'as a prop. Use prop default value instead.',
105+
'as a prop. To provide default value for a prop, use the "default" ' +
106+
'prop option; if you want to pass prop values to an instantiation ' +
107+
'call, use the "propsData" option.',
109108
this
110109
)
111110
}

test/unit/specs/compiler/compile_spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe('Compile', function () {
1616
directiveBind = jasmine.createSpy('bind')
1717
directiveTeardown = jasmine.createSpy('teardown')
1818
vm = {
19+
$options: {},
1920
_data: {},
2021
_directives: [],
2122
_bindDir: function (descriptor, node) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,13 +601,13 @@ describe('prop', function () {
601601
expect('already defined as a prop').toHaveBeenWarned()
602602
})
603603

604-
it('should not warn data fields already defined as a prop if it is from instantiation call', function () {
604+
it('propsData options', function () {
605605
var vm = new Vue({
606606
el: el,
607607
props: {
608608
a: null
609609
},
610-
data: {
610+
propsData: {
611611
a: 123
612612
}
613613
})

test/unit/specs/instance/state_spec.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,6 @@ describe('Instance state initialization', function () {
6666
expect(_.hasOwn(vm, 'c')).toBe(true)
6767
})
6868

69-
it('should use default prop value if prop not provided', function () {
70-
var vm = new Vue({
71-
el: document.createElement('div'),
72-
props: ['c'],
73-
data: {
74-
c: 1
75-
}
76-
})
77-
expect(vm.c).toBe(1)
78-
})
79-
8069
it('external prop should overwrite default value', function () {
8170
var el = document.createElement('div')
8271
el.setAttribute('v-bind:c', '2')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy