From 465f557eb10c8d6e2328fd2598d4d5a232d9e450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=81=93=E7=8E=84?= Date: Thu, 7 Jun 2018 02:18:51 +0800 Subject: [PATCH] feat(core): add staticClass merge from slot --- src/compiler/codegen/index.js | 11 ++++++++- .../instance/render-helpers/render-slot.js | 3 +++ test/unit/features/directives/class.spec.js | 23 +++++++++++++++++++ test/unit/modules/compiler/codegen.spec.js | 7 ++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/compiler/codegen/index.js b/src/compiler/codegen/index.js index 1c912c4d96f..3685408d994 100644 --- a/src/compiler/codegen/index.js +++ b/src/compiler/codegen/index.js @@ -457,7 +457,16 @@ function genSlot (el: ASTElement, state: CodegenState): string { const slotName = el.slotName || '"default"' const children = genChildren(el, state) let res = `_t(${slotName}${children ? `,${children}` : ''}` - const attrs = el.attrs && `{${el.attrs.map(a => `${camelize(a.name)}:${a.value}`).join(',')}}` + let attrs + if (el.attrs) { + attrs = `{${el.attrs.map(a => `${camelize(a.name)}:${a.value}`).join(',')}` + } + if (el.staticClass) { + attrs = attrs ? `${attrs},` : '{' + `class:${el.staticClass}` + } + if (attrs) { + attrs += '}' + } const bind = el.attrsMap['v-bind'] if ((attrs || bind) && !children) { res += `,null` diff --git a/src/core/instance/render-helpers/render-slot.js b/src/core/instance/render-helpers/render-slot.js index a58daa74e62..55a8388842a 100644 --- a/src/core/instance/render-helpers/render-slot.js +++ b/src/core/instance/render-helpers/render-slot.js @@ -37,6 +37,9 @@ export function renderSlot ( ) } slotNodes._rendered = true + if (props && props.class) { + slotNodes[0].data.staticClass += ` ${props.class}` + } } nodes = slotNodes || fallback } diff --git a/test/unit/features/directives/class.spec.js b/test/unit/features/directives/class.spec.js index c1390697ed9..27ab90d1e1e 100644 --- a/test/unit/features/directives/class.spec.js +++ b/test/unit/features/directives/class.spec.js @@ -138,6 +138,29 @@ describe('Directive v-bind:class', () => { }).then(done) }) + it('class merge from slot', done => { + const vm = new Vue({ + template: ` + +
+ some text +
+
+ `, + components: { + component1: { + template: ` +
+ +
+ ` + } + } + }).$mount() + expect(vm.$el.children[0].className).toBe('staticClass fromSlot') + done() + }) + it('deep update', done => { const vm = new Vue({ template: '
', diff --git a/test/unit/modules/compiler/codegen.spec.js b/test/unit/modules/compiler/codegen.spec.js index 93a27f3b245..68fa9d2282d 100644 --- a/test/unit/modules/compiler/codegen.spec.js +++ b/test/unit/modules/compiler/codegen.spec.js @@ -208,6 +208,13 @@ describe('codegen', () => { ) }) + it('generate slot with class', () => { + assertCodegen( + '
', + `with(this){return _c('div',[_t("default",null,{class:"class1"})],2)}` + ) + }) + it('generate class binding', () => { // static assertCodegen(