Просмотр исходного кода

fix(runtime-core): support object syntax for class (#215)

Illya Klymov 6 лет назад
Родитель
Сommit
e32da9169b
2 измененных файлов с 18 добавлено и 5 удалено
  1. 2 5
      packages/runtime-core/src/vnode.ts
  2. 16 0
      packages/vue/__tests__/index.spec.ts

+ 2 - 5
packages/runtime-core/src/vnode.ts

@@ -4,8 +4,7 @@ import {
   isString,
   isObject,
   EMPTY_ARR,
-  extend,
-  PatchFlags
+  extend
 } from '@vue/shared'
 import {
   ComponentInternalInstance,
@@ -146,9 +145,7 @@ export function createVNode(
     if (isReactive(props) || SetupProxySymbol in props) {
       props = extend({}, props)
     }
-    // class normalization only needed if the vnode isn't generated by
-    // compiler-optimized code
-    if (props.class != null && !(patchFlag & PatchFlags.CLASS)) {
+    if (props.class != null) {
       props.class = normalizeClass(props.class)
     }
     let { style } = props

+ 16 - 0
packages/vue/__tests__/index.spec.ts

@@ -13,3 +13,19 @@ it('should support on-the-fly template compilation', () => {
   createApp().mount(App, container)
   expect(container.innerHTML).toBe(`0`)
 })
+
+it('should correctly normalize class with on-the-fly template compilation', () => {
+  const container = document.createElement('div')
+  const App = {
+    template: `<div :class="{ test: demoValue, test2: !demoValue }"></div>`,
+    data() {
+      return {
+        demoValue: true
+      }
+    }
+  }
+  createApp().mount(App, container)
+  const classes = container.firstElementChild!.classList
+  expect(classes.contains('test')).toBe(true)
+  expect(classes.contains('test2')).toBe(false)
+})