Skip to content
On this page

API

VueToPrint Component

参数

属性名类型描述
bodyClass?string一个或多个类名,用空格分隔,传递到打印窗口
contentHTMLElement要打印的 DOM 元素。
copyStyles?boolean将父窗口中<head>内的所有<style><link type="stylesheet" />标签复制到打印窗口中。 (默认值: true)
documentTitle?string在保存为文件时设置打印的标题
fonts?{ family: string, source: string; weight?: string; style?: string; }[]可以选择提供字体列表,这些字体将加载到打印的 iframe 中。如果使用自定义字体,则这很有用
onAfterPrint?function在打印对话框关闭后触发的回调函数,无论用户选择打印还是取消
onBeforeGetContent?function在库收集页面内容之前触发的回调函数。返回 void 或 Promise。这可用于在打印之前更改页面上的内容
onBeforePrint?function在打印之前触发的回调函数。返回 void 或 Promise。注意:此函数在打印之前立即运行,但在收集页面内容之后运行。要在打印之前修改内容,请改用onBeforeGetContent
onPrintError?function如果有严重的打印错误,无法继续打印,则调用回调函数(签名:function(errorLocation: 'onBeforePrint' | 'onBeforeGetContent' | 'print', error: Error))。目前仅限于onBeforeGetContentonBeforePrintprint中的 Promise 拒绝。使用此功能再次尝试打印。errorLocation将告诉您 Promise 在哪个回调中被拒绝
pageStyle?string or function我们设置了一些基本样式,以帮助改善页面打印。使用此选项覆盖它们并提供自己的样式。如果作为函数给出,则必须返回一个string
print?function如果传递,则使用此函数而不是window.print来打印内容。此函数传递用于收集打印内容的 iframe HTMLIFrameElement。完成后,此函数必须返回一个 Promise。使用此功能在非浏览器环境(如 Electron)中打印
removeAfterPrint?boolean执行操作后删除打印 iframe。默认为false
suppressErrors?boolean当传递时,防止console记录错误
nonce?string为 CSP(内容安全策略)设置 nonce 属性,以使脚本和样式元素获得白名单

Slots

名称类型描述
defaultv-slot="{ handlePrint: () => void }"返回 Vue 节点或元素的函数。handlePrint是触发打印对话框的函数。
trigger?v-slot:trigger返回 Vue 节点或元素的函数。注意:在底层,我们将自定义的onClick属性注入返回的组件/元素中。因此,请不要为由trigger返回的根节点提供onClick属性,因为它将被覆盖

useVueToPrint Hook

类型定义

typescript
declare function useVueToPrint(options: PublicUseVueToPrintProps): { handlePrint: () => void };

// content 参数是必须的,其他参数是可选的
declare type PublicUseVueToPrintProps = Partial<Omit<UseVueToPrintProps, "content">> & Pick<UseVueToPrintProps, "content">;

// 参数说明见上方 VueToPrint Component 参数说明
export interface UseVueToPrintProps {
    bodyClass: MaybeRefOrGetter<string>;
    content: MaybeRefOrGetter<HTMLElement>;
    copyStyles: MaybeRefOrGetter<boolean>;
    documentTitle: MaybeRefOrGetter<string>;
    fonts: MaybeRefOrGetter<Font[]>;
    onAfterPrint: MaybeRefOrGetter<() => void>;
    onBeforeGetContent: MaybeRefOrGetter<() => void | Promise<void>>;
    onBeforePrint: MaybeRefOrGetter<() => void | Promise<void>>;
    onPrintError: MaybeRefOrGetter<(errorLocation: "onBeforeGetContent" | "onBeforePrint" | "print", error: Error) => void>;
    pageStyle: MaybeRefOrGetter<string | PropertyFunction<string>>;
    print: MaybeRefOrGetter<(target: HTMLIFrameElement) => Promise<void>>;
    removeAfterPrint: MaybeRefOrGetter<boolean>;
    suppressErrors: MaybeRefOrGetter<boolean>;
    nonce: MaybeRefOrGetter<string>;
}