/** * lib/base.js —— 页面基类:async setup() 后渲染;断开后挂起的 setup 结果不再渲染。 */ import { LitElement } from 'lit'; export class PhPage extends LitElement { createRenderRoot() { return this; } // 页面组件用全局主题变量,不用 Shadow DOM render() { return null; } async setup() { /* 子类覆盖:加载数据 */ } connectedCallback() { super.connectedCallback(); if (!this._ready) { this._alive = true; this._ready = true; this.setup().then(() => { if (this._alive) this.requestUpdate(); }).catch((err) => { if (this._alive) { this.error = err; this.requestUpdate(); } }); } } disconnectedCallback() { super.disconnectedCallback(); this._alive = false; } } export class PhError extends HTMLElement { connectedCallback() { if (this._done) return; this._done = true; const msg = this.error?.message || String(this.error || '未知错误'); this.innerHTML = `
FmodeCode · Pocket Hardware Lab

页面加载失败

${msg.replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c]))}

返回展厅
`; } } customElements.define('ph-error', PhError);