فهرست منبع

allow expressions in root instance props (fix #2278)

Evan You 10 سال پیش
والد
کامیت
5397333987
2فایلهای تغییر یافته به همراه14 افزوده شده و 14 حذف شده
  1. 9 12
      src/compiler/compile-props.js
  2. 5 2
      test/unit/specs/compiler/compile_spec.js

+ 9 - 12
src/compiler/compile-props.js

@@ -151,25 +151,22 @@ function makePropsLinkFn (props) {
         initProp(vm, prop, getDefault(vm, options))
       } else if (prop.dynamic) {
         // dynamic prop
-        if (vm._context) {
-          if (prop.mode === propBindingModes.ONE_TIME) {
-            // one time binding
-            value = (scope || vm._context).$get(prop.parentPath)
-            initProp(vm, prop, value)
-          } else {
+        if (prop.mode === propBindingModes.ONE_TIME) {
+          // one time binding
+          value = (scope || vm._context || vm).$get(prop.parentPath)
+          initProp(vm, prop, value)
+        } else {
+          if (vm._context) {
             // dynamic binding
             vm._bindDir({
               name: 'prop',
               def: propDef,
               prop: prop
             }, null, null, scope) // el, host, scope
+          } else {
+            // root instance
+            initProp(vm, prop, vm.$get(prop.parentPath))
           }
-        } else {
-          process.env.NODE_ENV !== 'production' && warn(
-            'Cannot bind dynamic prop on a root instance' +
-            ' with no parent: ' + prop.name + '="' +
-            raw + '"'
-          )
         }
       } else if (prop.optimizedLiteral) {
         // optimized literal, cast it and just set once

+ 5 - 2
test/unit/specs/compiler/compile_spec.js

@@ -28,6 +28,9 @@ describe('Compile', function () {
           _teardown: directiveTeardown
         })
       },
+      $get: function (exp) {
+        return (new Vue()).$get(exp)
+      },
       $eval: function (value) {
         return data[value]
       },
@@ -341,11 +344,11 @@ describe('Compile', function () {
     var context = vm._context
     vm._context = null
     el.setAttribute('v-bind:a', '"hi"')
-    el.setAttribute(':b', 'hi')
+    el.setAttribute(':b', '[1,2,3]')
     compiler.compileAndLinkProps(vm, el, { a: null, b: null })
     expect(vm._bindDir.calls.count()).toBe(0)
     expect(vm._data.a).toBe('hi')
-    expect(hasWarned('Cannot bind dynamic prop on a root')).toBe(true)
+    expect(vm._data.b.join(',')).toBe('1,2,3')
     // restore parent mock
     vm._context = context
   })