From ac0bd6e3914e6435388998f0f02d0a0ca18195ef Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 17 Jun 2022 16:06:57 +0800 Subject: [PATCH] fix(mp-baidu): reSetTemplateRef (#3615) --- packages/uni-mp-vue/lib/vue.runtime.esm.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/uni-mp-vue/lib/vue.runtime.esm.js b/packages/uni-mp-vue/lib/vue.runtime.esm.js index f87d65da7..cc22c1a78 100644 --- a/packages/uni-mp-vue/lib/vue.runtime.esm.js +++ b/packages/uni-mp-vue/lib/vue.runtime.esm.js @@ -4805,11 +4805,28 @@ function setRef(instance, isUnmount = false) { if (isUnmount) { return $templateRefs.forEach(templateRef => setTemplateRef(templateRef, null, setupState)); } - const doSet = () => { + const check = $mpPlatform === 'mp-baidu' || $mpPlatform === 'mp-toutiao'; + const doSetByRefs = (refs) => { const mpComponents = $scope .selectAllComponents('.r') .concat($scope.selectAllComponents('.r-i-f')); - $templateRefs.forEach(templateRef => setTemplateRef(templateRef, findComponentPublicInstance(mpComponents, templateRef.i), setupState)); + return refs.filter(templateRef => { + const refValue = findComponentPublicInstance(mpComponents, templateRef.i); + // 部分平台,在一些 if 条件下,部分 slot 组件初始化会被延迟到下一次渲染,需要二次检测 + if (check && refValue === null) { + return true; + } + setTemplateRef(templateRef, refValue, setupState); + return false; + }); + }; + const doSet = () => { + const refs = doSetByRefs($templateRefs); + if (refs.length) { + setTimeout(() => { + doSetByRefs(refs); + }, 10); + } }; if ($scope._$setRef) { $scope._$setRef(doSet); -- GitLab