From f022135d7c9d52efa002a525dd9cb1636995eb54 Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 28 Oct 2021 18:15:40 +0800 Subject: [PATCH] fix: slot with v-for question/130258 --- .../__tests__/compiler-app-plus.service.spec.js | 11 +++++++++++ packages/uni-template-compiler/lib/app/service.js | 3 +++ 2 files changed, 14 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 7d5fe816c..7b95a1b98 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 @@ -193,6 +193,17 @@ describe('codegen', () => { `with(this){return _c('p',{slot:"one"})}` ) }) + + it('generate slot with v-for', () => { + assertCodegen( + '
', + `with(this){return _c('div',[_l((_$s(1,'f',{forItems:list})),function(item,$10,$20,$30){return _t("default",null,{"_i":("1-"+$30),"key":_$s(1,'f',{forIndex:$20,key:1+'-'+$30})})})],2)}` + ) + assertCodegen( + '
', + `with(this){return _c('div',[_l((_$s(1,'f',{forItems:list})),function(item,$10,$20,$30){return [_t("default",null,{"_i":("2-"+$30),"key":_$s(1,'f',{forIndex:$20,keyIndex:0,key:1+'-0'+$30})})]})],2)}` + ) + }) it('generate scoped slot', () => { assertCodegen( diff --git a/packages/uni-template-compiler/lib/app/service.js b/packages/uni-template-compiler/lib/app/service.js index 85a0d29b3..72d36fb73 100644 --- a/packages/uni-template-compiler/lib/app/service.js +++ b/packages/uni-template-compiler/lib/app/service.js @@ -72,6 +72,9 @@ function parseKey (el, isScopedSlot) { const keyIndex = forEl.children.indexOf(el) el.key = genVar(V_FOR, `{forIndex:${it},keyIndex:${keyIndex},key:${el.key}}`) } + if (el.tag === 'slot') { + el.attrs.push({ name: 'key', value: el.key }) + } } function parseComponentAttrs (el, genVar) { -- GitLab