From c416622f729986695f812d4adb8669f0523f43f3 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 22 Mar 2023 18:49:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dref=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E4=B8=A2=E5=A4=B1=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/html.js | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 93df4ac..16d44ec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bd/core", - "version": "1.5.9", + "version": "1.5.10", "type": "module", "description": "百搭UI组件库的核心", "main": "dist/index.js", diff --git a/src/html.js b/src/html.js index 986a15b..e8e0a6f 100644 --- a/src/html.js +++ b/src/html.js @@ -286,6 +286,16 @@ class TemplateInstance { get _$isConnected() { return this._$parent._$isConnected } + + #checkRef(node, walker, options) { + do { + if (node.nodeType === 1 && node.getAttribute('ref')) { + options.host.$refs[node.getAttribute('ref')] = node + node.removeAttribute('ref') + } + } while ((node = walker.nextNode()) !== null) + } + _clone(options) { let { el: { content }, @@ -303,12 +313,7 @@ class TemplateInstance { let templatePart = parts[0] // 没有动态绑定时, 查一遍是否有ref属性 if (templatePart === void 0) { - do { - if (node.nodeType === 1 && node.getAttribute('ref')) { - options.host.$refs[node.getAttribute('ref')] = node - node.removeAttribute('ref') - } - } while ((node = walker.nextNode()) !== null) + this.#checkRef(node, walker, options) } else { while (templatePart !== void 0) { if (nodeIndex === templatePart.index) { @@ -329,6 +334,7 @@ class TemplateInstance { if (node.nodeType === 1 && node.getAttribute('ref')) { if (options.host.$refs) { options.host.$refs[node.getAttribute('ref')] = node + node.removeAttribute('ref') } } this._parts.push(part) @@ -344,6 +350,8 @@ class TemplateInstance { nodeIndex++ } } + // 再检查剩下没有动态绑定的节点 + this.#checkRef(node, walker, options) } return fragment