| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- /**
- * 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 = `<div style="padding:120px 24px;text-align:center;font-family:var(--fx-font)">
- <div style="font-size:12px;letter-spacing:.12em;color:#86868b;margin-bottom:16px">FmodeCode · Pocket Hardware Lab</div>
- <h1 style="font-size:28px;margin:0 0 12px">页面加载失败</h1>
- <p style="color:#6e6e73;max-width:36em;margin:0 auto 28px">${msg.replace(/[&<>]/g, (c) => ({ '&': '&', '<': '<', '>': '>' }[c]))}</p>
- <a href="#/" style="color:#0071e3">返回展厅</a></div>`;
- }
- }
- customElements.define('ph-error', PhError);
|