base.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * lib/base.js —— 页面基类:async setup() 后渲染;断开后挂起的 setup 结果不再渲染。
  3. */
  4. import { LitElement } from 'lit';
  5. export class PhPage extends LitElement {
  6. createRenderRoot() { return this; } // 页面组件用全局主题变量,不用 Shadow DOM
  7. render() { return null; }
  8. async setup() { /* 子类覆盖:加载数据 */ }
  9. connectedCallback() {
  10. super.connectedCallback();
  11. if (!this._ready) {
  12. this._alive = true;
  13. this._ready = true;
  14. this.setup().then(() => {
  15. if (this._alive) this.requestUpdate();
  16. }).catch((err) => {
  17. if (this._alive) { this.error = err; this.requestUpdate(); }
  18. });
  19. }
  20. }
  21. disconnectedCallback() {
  22. super.disconnectedCallback();
  23. this._alive = false;
  24. }
  25. }
  26. export class PhError extends HTMLElement {
  27. connectedCallback() {
  28. if (this._done) return;
  29. this._done = true;
  30. const msg = this.error?.message || String(this.error || '未知错误');
  31. this.innerHTML = `<div style="padding:120px 24px;text-align:center;font-family:var(--fx-font)">
  32. <div style="font-size:12px;letter-spacing:.12em;color:#86868b;margin-bottom:16px">FmodeCode · Pocket Hardware Lab</div>
  33. <h1 style="font-size:28px;margin:0 0 12px">页面加载失败</h1>
  34. <p style="color:#6e6e73;max-width:36em;margin:0 auto 28px">${msg.replace(/[&<>]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;' }[c]))}</p>
  35. <a href="#/" style="color:#0071e3">返回展厅</a></div>`;
  36. }
  37. }
  38. customElements.define('ph-error', PhError);