From a6efaaabf0c060a755912f314a5643dbcdca115e Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 16 Mar 2023 16:31:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dobserver=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=BA=A2=E5=87=BA=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/constants.js | 7 +------ src/index.js | 14 +++++++++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 2cb0d58..175fd8d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bd/core", - "version": "1.4.0", + "version": "1.4.1", "type": "module", "description": "百搭UI组件库的核心", "main": "dist/index.js", diff --git a/src/constants.js b/src/constants.js index 3ecec6f..116ac58 100644 --- a/src/constants.js +++ b/src/constants.js @@ -132,11 +132,9 @@ export function fixedValue(value, options) { switch (options.type) { case Number: return value === null ? null : +value || 0 - break case Boolean: return value === false ? false : value !== null - break case Object: if (typeof value === 'object') { @@ -148,7 +146,6 @@ export function fixedValue(value, options) { return {} } } - break case Array: if (typeof value === 'object') { @@ -160,10 +157,8 @@ export function fixedValue(value, options) { return [] } } - break default: - return value === null ? null : value + '' - break + return value === null || value === void 0 ? null : value + '' } } diff --git a/src/index.js b/src/index.js index a4c4123..1a13700 100644 --- a/src/index.js +++ b/src/index.js @@ -56,14 +56,14 @@ export class Component extends HTMLElement { return } this[key] = value + this.#requestUpdate(name, oldValue) if (options.observer) { options.observer.call(this, value, oldValue) } - this.#requestUpdate(name, oldValue) }, enumerable: false } - this.prototype[key] = options.default + this[__props__].set(name, options) Object.defineProperty(this.prototype, name, descriptor) } @@ -102,7 +102,12 @@ export class Component extends HTMLElement { this.root = this.shadowRoot || this.attachShadow({ mode: 'open' }) this[__changed_props__] = new Map() // 记录本次变化的属性 - // 手动执行一次渲染 + // 初始化 props + this.constructor[__props__].forEach((options, prop) => { + this[prop] = options.default + }) + + // 若无定义props时, 手动执行一次渲染 if (this[__pending__] === false) { this[__pending__] = true nextTick(_ => this[__updated__]()) @@ -144,6 +149,9 @@ export class Component extends HTMLElement { return } + // name === 'icon' && + // console.log(name, '>>>', options.default, ':::', this[name]) + switch (options.type) { case Number: case String: