From 0eecaa22dc09bb5d7c9913800677ac6da8a83698 Mon Sep 17 00:00:00 2001 From: yutent Date: Mon, 20 Mar 2023 11:29:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B2=A1=E6=9C=89=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E7=BB=91=E5=AE=9A=E6=97=B6,=20ref=E4=B8=8D=E7=94=9F?= =?UTF-8?q?=E6=95=88=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/html.js | 76 ++++++++++++++++++++++++++++------------------------ 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 93e1f35..69d884f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bd/core", - "version": "1.5.3", + "version": "1.5.4", "type": "module", "description": "百搭UI组件库的核心", "main": "dist/index.js", diff --git a/src/html.js b/src/html.js index 4ac62da..986a15b 100644 --- a/src/html.js +++ b/src/html.js @@ -301,42 +301,51 @@ class TemplateInstance { let nodeIndex = 0 let partIndex = 0 let templatePart = parts[0] - - while (templatePart !== void 0) { - if (nodeIndex === templatePart.index) { - let part - if (templatePart.type === CHILD_PART) { - part = new ChildPart(node, node.nextSibling, this, options) - } else if (templatePart.type === ATTRIBUTE_PART) { - part = new templatePart.ctor( - node, - templatePart.name, - templatePart.strings, - this, - options - ) - } else if (templatePart.type === ELEMENT_PART) { - part = new ElementPart(node, this, options) - } - // console.log(node) + // 没有动态绑定时, 查一遍是否有ref属性 + if (templatePart === void 0) { + do { if (node.nodeType === 1 && node.getAttribute('ref')) { - if (options.host.$refs) { - options.host.$refs[node.getAttribute('ref')] = node - } + options.host.$refs[node.getAttribute('ref')] = node + node.removeAttribute('ref') + } + } while ((node = walker.nextNode()) !== null) + } else { + while (templatePart !== void 0) { + if (nodeIndex === templatePart.index) { + let part + if (templatePart.type === CHILD_PART) { + part = new ChildPart(node, node.nextSibling, this, options) + } else if (templatePart.type === ATTRIBUTE_PART) { + part = new templatePart.ctor( + node, + templatePart.name, + templatePart.strings, + this, + options + ) + } else if (templatePart.type === ELEMENT_PART) { + part = new ElementPart(node, this, options) + } + if (node.nodeType === 1 && node.getAttribute('ref')) { + if (options.host.$refs) { + options.host.$refs[node.getAttribute('ref')] = node + } + } + this._parts.push(part) + templatePart = parts[++partIndex] + } + if ( + nodeIndex !== + (templatePart === null || templatePart === void 0 + ? void 0 + : templatePart.index) + ) { + node = walker.nextNode() + nodeIndex++ } - this._parts.push(part) - templatePart = parts[++partIndex] - } - if ( - nodeIndex !== - (templatePart === null || templatePart === void 0 - ? void 0 - : templatePart.index) - ) { - node = walker.nextNode() - nodeIndex++ } } + return fragment } _update(values) { @@ -410,7 +419,6 @@ class ChildPart { _commitNode(value) { if (this._$committedValue !== value) { this.#clear() - this._$committedValue = this._insert(value) } } @@ -669,8 +677,6 @@ export function render(value, container, options = {}) { } part._$setValue(value) - // console.log(container, options.host, options.host.$refs) - return part }