From 439287da9c15494f0ddf54f25baf4bbb8a83b690 Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 21 Mar 2023 15:43:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E6=9C=BA=E5=88=B6,=20=E4=BB=A5=E5=85=BC=E5=AE=B9vue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/index.js | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 69260a1..5ef0a1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bd/core", - "version": "1.5.5", + "version": "1.5.6", "type": "module", "description": "百搭UI组件库的核心", "main": "dist/index.js", diff --git a/src/index.js b/src/index.js index ae1a6f8..0afd1fd 100644 --- a/src/index.js +++ b/src/index.js @@ -65,6 +65,7 @@ export class Component extends HTMLElement { } this[__props__].set(name, options) + Object.defineProperty(this.prototype, name, descriptor) } @@ -94,7 +95,10 @@ export class Component extends HTMLElement { super() this[__pending__] = false this[__mounted__] = false - this[__init__]() + // 这里提前定义一次, 是为了在connectedCallback之前, 就已有赋值时报错的bug + this[__changed_props__] = new Map() // 记录一次渲染周期内变化的属性 + this.root = this.shadowRoot || this.attachShadow({ mode: 'open' }) + Object.defineProperty(this, '$refs', { value: Object.create(null), enumerable: false @@ -104,12 +108,11 @@ export class Component extends HTMLElement { } [__init__]() { - this.root = this.shadowRoot || this.attachShadow({ mode: 'open' }) - - this[__changed_props__] = new Map() // 记录本次变化的属性 + // 这里重新赋值一次, 用于清除掉因为 observer 修正产生的变化 + this[__changed_props__] = new Map() // 初始化 props this.constructor[__props__].forEach((options, prop) => { - this[prop] = options.default + this[prop] = this[prop] === void 0 ? options.default : this[prop] }) // 若无定义props时, 手动执行一次渲染 @@ -124,6 +127,7 @@ export class Component extends HTMLElement { } connectedCallback() { + this[__init__]() adoptStyles(this.root, this.constructor.styles) this[__children__]?.setConnected(true)