调整初始化机制, 以兼容vue

pull/1/head
yutent 2023-03-21 15:43:41 +08:00
parent 406f581ecf
commit 439287da9c
2 changed files with 10 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@bd/core",
"version": "1.5.5",
"version": "1.5.6",
"type": "module",
"description": "百搭UI组件库的核心",
"main": "dist/index.js",

View File

@ -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)