From 81f8fd45eb7eb94e696d0e2a357ca20b84223974 Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 21 Mar 2023 19:48:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=B8=B2=E6=9F=93=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index 34c89f0..5569426 100644 --- a/src/index.js +++ b/src/index.js @@ -121,7 +121,9 @@ export class Component extends HTMLElement { this[__changed_props__] = new Map() // 初始化 props this.constructor[__props__].forEach((options, prop) => { + // console.log(this, prop, this[prop], options.default) this[prop] = this[prop] === void 0 ? options.default : this[prop] + // console.log('赋值后:', prop, this[prop]) }) // 若无定义props时, 手动执行一次渲染 @@ -217,28 +219,27 @@ export class Component extends HTMLElement { // 确认更新到视图 [__updated__]() { - if (this[__in_tree__] === false) { - this[__in_tree__] = document.body.contains(this) - } - - if (this[__in_tree__]) { - if (this[__pending__]) { - let props = this[__changed_props__] - this[__render__]() - this[__feedback__](props) - this[__clear_update__]() - } + if (this[__pending__]) { + let props = this[__changed_props__] + this[__render__]() + this[__feedback__](props) + this[__clear_update__]() } } // 更新回调反馈 [__feedback__](props) { - // 初始化时不触发updated回调 - if (this[__mounted__] === false) { - this[__mounted__] = true - this.mounted() - } else { - this.updated(props) + if (this[__in_tree__] === false) { + this[__in_tree__] = document.body.contains(this) + } + if (this[__in_tree__]) { + // 初始化时不触发updated回调 + if (this[__mounted__] === false) { + this[__mounted__] = true + this.mounted() + } else { + this.updated(props) + } } }