From 9f22b6373592c316f70b16a84b1f77f2b590ee37 Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 23 Jun 2022 20:06:24 +0800 Subject: [PATCH] fix(App): slot name dynamic data (question/95109) --- .../__tests__/compiler-app-plus.service.spec.js | 4 ++++ .../__tests__/compiler-app-plus.view.spec.js | 4 ++++ .../uni-template-compiler/lib/app/parser/base-parser.js | 8 ++++++++ packages/uni-template-compiler/lib/app/service.js | 2 ++ packages/uni-template-compiler/lib/app/util.js | 2 ++ packages/uni-template-compiler/lib/app/view.js | 2 ++ 6 files changed, 22 insertions(+) diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js index 7f6795a90..b4e280182 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js @@ -178,6 +178,10 @@ describe('codegen', () => { '
', `with(this){return _c('div',[_t("one",null,{"_i":1})],2)}` ) + assertCodegen( + '
', + `with(this){return _c('div',[_t(_$s(1,'name',one),null,{"_i":1})],2)}` + ) }) it('generate slot fallback content', () => { diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js index 251885226..b15a153c0 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js @@ -178,6 +178,10 @@ describe('codegen', () => { '
', `with(this){return _c('div',{attrs:{"_i":0}},[_t("one",null,{"_i":1})],2)}` ) + assertCodegen( + '
', + `with(this){return _c('div',{attrs:{"_i":0}},[_t(_$g(1,'name'),null,{"_i":1})],2)}` + ) }) it('generate slot fallback content', () => { diff --git a/packages/uni-template-compiler/lib/app/parser/base-parser.js b/packages/uni-template-compiler/lib/app/parser/base-parser.js index 5094693fb..1bca0b234 100644 --- a/packages/uni-template-compiler/lib/app/parser/base-parser.js +++ b/packages/uni-template-compiler/lib/app/parser/base-parser.js @@ -2,6 +2,7 @@ const { ID, C_IS, C_REF, + C_NAME, V_IF, V_FOR, V_ELSE_IF, @@ -16,6 +17,12 @@ function parseRef (el, genVar) { } } +function parseSlotName (el, genVar) { + if (el.slotName && isVar(el.slotName)) { + el.slotName = genVar(C_NAME, el.slotName) + } +} + function parseIs (el, genVar) { if (!el.component) { return @@ -106,6 +113,7 @@ function parseText (el, parent, state) { module.exports = { parseIs, parseRef, + parseSlotName, parseIf, parseFor, parseText, diff --git a/packages/uni-template-compiler/lib/app/service.js b/packages/uni-template-compiler/lib/app/service.js index 72d36fb73..da63bc1c5 100644 --- a/packages/uni-template-compiler/lib/app/service.js +++ b/packages/uni-template-compiler/lib/app/service.js @@ -18,6 +18,7 @@ const { const { parseIs, parseRef, + parseSlotName, parseIf, parseFor, parseText, @@ -137,6 +138,7 @@ function transformNode (el, parent, state, isScopedSlot) { parseIs(el, genVar) parseRef(el, genVar) + parseSlotName(el, genVar) parseFor(el, createGenVar, isScopedSlot, checkAutoFill(el)) parseKey(el, isScopedSlot) diff --git a/packages/uni-template-compiler/lib/app/util.js b/packages/uni-template-compiler/lib/app/util.js index 3de296a61..c259090ee 100644 --- a/packages/uni-template-compiler/lib/app/util.js +++ b/packages/uni-template-compiler/lib/app/util.js @@ -15,6 +15,7 @@ const GET_CHANGE_DATA = '_$gc' // wxs const C_IS = 'is' const C_SLOT_TARGET = 'st' const C_REF = 'ref' +const C_NAME = 'name' const V_FOR = 'f' const V_IF = 'i' @@ -268,6 +269,7 @@ function addHandler (el, name, value, important) { module.exports = { C_IS, C_REF, + C_NAME, V_FOR, V_IF, V_ELSE_IF, diff --git a/packages/uni-template-compiler/lib/app/view.js b/packages/uni-template-compiler/lib/app/view.js index b0ab64641..8a568e4c5 100644 --- a/packages/uni-template-compiler/lib/app/view.js +++ b/packages/uni-template-compiler/lib/app/view.js @@ -13,6 +13,7 @@ const { const { parseIs, parseRef, + parseSlotName, parseIf, parseFor, parseText, @@ -130,6 +131,7 @@ function transformNode (el, parent, state, isScopedSlot) { parseIs(el, genVar) parseRef(el, genVar) + parseSlotName(el, genVar) if (parseFor(el, createGenVar, isScopedSlot)) { if (el.alias[0] === '{') { //
  • el.alias = '$item' -- GitLab