Jelajahi Sumber

support :style binding to strings (fix #3379)

Evan You 9 tahun lalu
induk
melakukan
69ba01e7d4

+ 7 - 0
src/platforms/web/runtime/modules/style.js

@@ -28,6 +28,13 @@ function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
   const elm: any = vnode.elm
   const oldStyle: any = oldVnode.data.style || {}
   let style: any = vnode.data.style || {}
+
+  // handle string
+  if (typeof style === 'string') {
+    elm.setAttribute('style', style)
+    return
+  }
+
   const needClone = style.__ob__
 
   // handle array syntax

+ 7 - 0
test/unit/features/directives/style.spec.js

@@ -30,6 +30,13 @@ describe('Directive v-bind:style', () => {
     }).$mount()
   })
 
+  it('string', done => {
+    vm.styles = 'color:red;'
+    waitForUpdate(() => {
+      expect(vm.$el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
+    }).then(done)
+  })
+
   it('plain object', done => {
     vm.styles = { color: 'red' }
     waitForUpdate(() => {