优化组件的初始化

pull/1/head
yutent 2023-03-16 14:32:35 +08:00
parent a57016e14e
commit 3bc02e2e13
1 changed files with 4 additions and 9 deletions

View File

@ -63,7 +63,7 @@ export class Component extends HTMLElement {
}, },
enumerable: false enumerable: false
} }
this.prototype[key] = options.default
this[__props__].set(name, options) this[__props__].set(name, options)
Object.defineProperty(this.prototype, name, descriptor) Object.defineProperty(this.prototype, name, descriptor)
} }
@ -102,11 +102,7 @@ export class Component extends HTMLElement {
this.root = this.shadowRoot || this.attachShadow({ mode: 'open' }) this.root = this.shadowRoot || this.attachShadow({ mode: 'open' })
this[__changed_props__] = new Map() // 记录本次变化的属性 this[__changed_props__] = new Map() // 记录本次变化的属性
// 初始化 props // 手动执行一次渲染
this.constructor[__props__].forEach((options, prop) => {
this[prop] = options.default
})
// 若无定义props时, 手动执行一次渲染
if (this[__pending__] === false) { if (this[__pending__] === false) {
this[__pending__] = true this[__pending__] = true
nextTick(_ => this[__updated__]()) nextTick(_ => this[__updated__]())
@ -132,7 +128,7 @@ export class Component extends HTMLElement {
if (old === val) { if (old === val) {
return return
} }
this[__attr2prop__](name, val) this[__attr2prop__](name, val, old)
} }
/** /**
@ -173,9 +169,8 @@ export class Component extends HTMLElement {
* @param name<String> * @param name<String>
* @param value<String|Boolean|Number> * @param value<String|Boolean|Number>
*/ */
[__attr2prop__](name, value) { [__attr2prop__](name, value, old) {
let options = this.#getPropOptions(name) let options = this.#getPropOptions(name)
this[name] = fixedValue(value, options) this[name] = fixedValue(value, options)
} }