Ver código fonte

v-bind object should have lower priority than explicit bindings (fix #5150)

Evan You 9 anos atrás
pai
commit
a6e1ae0aac

+ 5 - 2
src/core/instance/render-helpers/bind-object-props.js

@@ -22,14 +22,17 @@ export function bindObjectProps (
       if (Array.isArray(value)) {
         value = toObject(value)
       }
+      let hash
       for (const key in value) {
         if (key === 'class' || key === 'style') {
-          data[key] = value[key]
+          hash = data
         } else {
           const type = data.attrs && data.attrs.type
-          const hash = asProp || config.mustUseProp(tag, type, key)
+          hash = asProp || config.mustUseProp(tag, type, key)
             ? data.domProps || (data.domProps = {})
             : data.attrs || (data.attrs = {})
+        }
+        if (!(key in hash)) {
           hash[key] = value[key]
         }
       }

+ 23 - 0
test/unit/features/directives/bind.spec.js

@@ -166,6 +166,29 @@ describe('Directive v-bind', () => {
     }).then(done)
   })
 
+  it('bind object with overwrite', done => {
+    const vm = new Vue({
+      template: '<input v-bind="test" id="foo" :class="test.value">',
+      data: {
+        test: {
+          id: 'test',
+          class: 'ok',
+          value: 'hello'
+        }
+      }
+    }).$mount()
+    expect(vm.$el.getAttribute('id')).toBe('foo')
+    expect(vm.$el.getAttribute('class')).toBe('hello')
+    expect(vm.$el.value).toBe('hello')
+    vm.test.id = 'hi'
+    vm.test.value = 'bye'
+    waitForUpdate(() => {
+      expect(vm.$el.getAttribute('id')).toBe('foo')
+      expect(vm.$el.getAttribute('class')).toBe('bye')
+      expect(vm.$el.value).toBe('bye')
+    }).then(done)
+  })
+
   it('bind object with class/style', done => {
     const vm = new Vue({
       template: '<input class="a" style="color:red" v-bind="test">',