API 基础

稳定

API 概览与访问方式

稳定性:stable。本文以当前 bee-report 源码的默认导出和 builderProps 为准;部署平台额外包装的能力不等同于公共 API。

调用方式

NPM 工程:

import { api } from "bee-report";
import "bee-report/es/index.css";

已加载全局构建产物的页面:

const api = window.beeReport.api;

公开 ExportApi 包含 print、printAll、getReportHtmlContent、exportExcel、exportPdf、convertWidgetReportToMobile、mount、unmount。其中 getReportHtmlContent 与 convertWidgetReportToMobile 虽已实现,但放在高级与实验性 API,不作为稳定能力承诺。

通用调用约定

多数渲染和输出方法接收:

interface ApiOptions {
  reportOptions: ReportOptions;
  printOptions?: PrintOptions;
}

NPM 使用时建议显式传入 Vue 2 构造器;省略时实现读取 window.Vue。mount 缺少 Vue 返回 602,缺少挂载点返回 601。输出方法返回 Promise,实际成功值依具体方法和下游打印服务而定。

最小示例

const options = {
  reportOptions: {
    reportCode: "sales-summary",
    reportVersion: "1.0.0",
  },
};

const container = document.querySelector("#report-host");
if (!container) throw new Error("Missing #report-host");
const instance = await api.mount(options, container, Vue);
// 页面销毁时:api.unmount(container);

iframe 模式差异

mount({ iframe: true }) resolve 的是消息代理;读取和校验方法返回 Promise,setter 没有同步返回值。当前挂载流程还直接访问 contentWindow.document 并写 contentWindow.$$BEEREPORT_PARAMS,所以 iframe 必须与宿主页同源;跨域需先把源码改造成纯 postMessage 协议。非 iframe 模式 resolve 当前 Vue Builder 实例。详见运行时实例方法。

相关 API

reportOptions、printOptions、mount 与 unmount。

常见错误

不要混用不同页面注入的 Vue,不要把 printOptions 嵌进 reportOptions,也不要在 mount resolve 前调用实例方法。