From afe4d8e332657514174e4e06f4cdba5d7555d03f Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Thu, 2 Sep 2021 18:07:51 +0800 Subject: [PATCH] =?UTF-8?q?fix(mp-baidu):=20for=20=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=20s-key=20=E4=BD=BF=E7=94=A8=20trackBy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__tests__/compiler-mp-baidu.spec.js | 10 +++++----- .../uni-template-compiler/lib/template/traverse.js | 11 +++++++++-- packages/uni-template-compiler/lib/util.js | 2 ++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js b/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js index 9e05a96a0..4d719a64e 100644 --- a/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-mp-baidu.spec.js @@ -23,8 +23,8 @@ describe('mp:compiler-mp-baidu', () => { }) it('generate v-for directive', () => { assertCodegen( - '', - '' + '', + '' ) }) it('generate scoped slot', () => { @@ -125,15 +125,15 @@ describe('mp:compiler-mp-baidu', () => { ) assertCodegen( '', - '' + '' ) assertCodegen( '', - '' + '' ) assertCodegen( '', - '' + '' ) }) diff --git a/packages/uni-template-compiler/lib/template/traverse.js b/packages/uni-template-compiler/lib/template/traverse.js index 7303c990c..fbe27a806 100644 --- a/packages/uni-template-compiler/lib/template/traverse.js +++ b/packages/uni-template-compiler/lib/template/traverse.js @@ -428,8 +428,15 @@ function traverseRenderList (callExprNode, state) { const prefix = state.options.platform.directive + const isBaidu = state.options.platform.name === 'mp-baidu' + let forValue = genCode(callExprNode.arguments[0], isBaidu) + + if(isBaidu && forKey) { + forValue += ` trackBy ${getForKey(forKey, forIndex, state)}` + } + const attr = { - [prefix + 'for']: genCode(callExprNode.arguments[0]), + [prefix + 'for']: forValue, [prefix + 'for-item']: forItem } @@ -437,7 +444,7 @@ function traverseRenderList (callExprNode, state) { attr[prefix + 'for-index'] = forIndex } - if (forKey) { + if (forKey && !isBaidu) { const key = getForKey(forKey, forIndex, state) if (key) { attr[prefix + 'key'] = key diff --git a/packages/uni-template-compiler/lib/util.js b/packages/uni-template-compiler/lib/util.js index 505ed6183..99a3103c1 100644 --- a/packages/uni-template-compiler/lib/util.js +++ b/packages/uni-template-compiler/lib/util.js @@ -107,6 +107,7 @@ function getForKey (forKey, forIndex, state) { if (forKey) { if (t.isIdentifier(forKey)) { if (forIndex !== forKey.name) { // 非 forIndex + if (state.options.platform.name === 'mp-baidu') return getCode(forKey) return '*this' } else { // TODO @@ -114,6 +115,7 @@ function getForKey (forKey, forIndex, state) { return forKey.name } } else if (t.isMemberExpression(forKey)) { + if (state.options.platform.name === 'mp-baidu') return getCode(forKey) return forKey.property.name || forKey.property.value } else { state.tips.add(uniI18n.__('templateCompiler.noH5KeyNoSupportExpression', { 0: getCode(forKey), 1: 'https://uniapp.dcloud.io/use?id=key' })) -- GitLab