8000 allow expressions in root instance props (fix #2278) · ws2042/vue@5397333 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5397333

Browse files
committed
allow expressions in root instance props (fix vuejs#2278)
1 parent 244ea47 commit 5397333

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

src/compiler/compile-props.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,22 @@ function makePropsLinkFn (props) {
151151
initProp(vm, prop, getDefault(vm, options))
152152
} else if (prop.dynamic) {
153153
// dynamic prop
154-
if (vm._context) {
155-
if (prop.mode === propBindingModes.ONE_TIME) {
156-
// one time binding
157-
value = (scope || vm._context).$get(prop.parentPath)
158-
initProp(vm, prop, value)
159-
} else {
154+
if (prop.mode === propBindingModes.ONE_TIME) {
155+
// one time binding
156+
value = (scope || vm._context || vm).$get(prop.parentPath)
157+
initProp(vm, prop, value)
158+
} else {
159+
if (vm._context) {
160160
// dynamic binding
161161
vm._bindDir({
162162
name: 'prop',
163163
def: propDef,
164164
prop: prop
165165
}, null, null, scope) // el, host, scope
166+
} else {
167+
// root instance
168+
initProp(vm, prop, vm.$get(prop.parentPath))
166169
}
167-
} else {
168-
process.env.NODE_ENV !== 'production' && warn(
169-
'Cannot bind dynamic prop on a root instance' +
170-
' with no parent: ' + prop.name + '="' +
171-
raw + '"'
172-
)
173170
}
174171
} else if (prop.optimizedLiteral) {
175172
// optimized literal, cast it and just set once

test/unit/specs/compiler/compile_spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ describe('Compile', function () {
2828
_teardown: directiveTeardown
2929
})
3030
},
31+
$get: function (exp) {
32+
return (new Vue()).$get(exp)
33+
},
3134
$eval: function (value) {
3235
return data[value]
3336
},
@@ -341,11 +344,11 @@ describe('Compile', function () {
341344
var context = vm._context
342345
vm._context = null
343346
el.setAttribute('v-bind:a', '"hi"')
344-
el.setAttribute(':b', 'hi')
347+
el.setAttribute(':b', '[1,2,3]')
345348
compiler.compileAndLinkProps(vm, el, { a: null, b: null })
346349
expect(vm._bindDir.calls.count()).toBe(0)
347350
expect(vm._data.a).toBe('hi')
348-
expect(hasWarned('Cannot bind dynamic prop on a root')).toBe(true)
351+
expect(vm._data.b.join(',')).toBe('1,2,3')
349352
// restore parent mock
350353
vm._context = context
351354
})

0 commit comments

Comments
 (0)
0