AvueAvue
首页
  • 开发指南
  • Skill开发
  • 在线测试工具
  • Form组件
  • Crud组件
  • Default组件
  • Data组件
  • Component组件
产品
工作台
授权
联系
2.x文档
个人支付接口
首页
  • 开发指南
  • Skill开发
  • 在线测试工具
  • Form组件
  • Crud组件
  • Default组件
  • Data组件
  • Component组件
产品
工作台
授权
联系
2.x文档
个人支付接口
  • Article 文章
  • Group 分组容器
  • Title 标题文本
  • CountUp 数字动画
  • Comment 评论
  • Chat 客服聊天
  • Captcha 验证码
  • Contextmenu 右键菜单
  • Clipboard 复制文本
  • DialogForm 弹窗表单
  • Draggable 拖拽
  • Export Excel 导入与导出
  • Flow 流程
  • FilterBuilder 高级筛选
  • FileManager 文件管理
  • QRCode 二维码
  • Iframe 嵌入页面
  • ImagePreview 图片预览
  • Login 登录组件
  • License 授权书
  • Print 打印
  • Screenshot 页面截图
  • Search 标签搜索
  • Sign 电子签名
  • TextEllipsis 超出文本省略
  • Tabs 选项卡
  • Tree 树型
  • Video 摄像头
  • Calendar 日历组合示例

Print 打印

$Print(dom, options) 接受 CSS 选择器、HTMLElement 或含 $el 的组件实例。目标必须已经挂载;插件在独立 iframe 中准备内容,再打开浏览器打印界面。

效果预览可直接操作下方示例

通过选择器或元素 ref 打印同一张卡片。卡片内的操作提示带 no-print 类,不会出现在打印内容中。

正在加载示例…
<template>
  <div>
    <div class="print-actions">
      <el-button @click="print('selector')">按选择器打印</el-button>
      <el-button @click="print('element')">按元素打印</el-button>
    </div>
    <div :id="cardId" ref="card" class="print-card">
      <h3>服务确认单</h3>
      <p>编号:A001</p>
      <p>服务:系统安装与使用培训</p>
      <p class="no-print">此操作提示仅在页面显示,不会打印。</p>
    </div>
    <p role="status">{{ message }}</p>
  </div>
</template>

<script setup>
import { onBeforeUnmount, ref, useId } from 'vue'
import { $Print } from '@smallwei/avue'
const cardId = 'print-' + useId()
const card = ref(null)
const message = ref('点击按钮打开浏览器打印界面')
let active = true
function update(text) { if (active) message.value = text }
function print(mode) {
  try {
    $Print(mode === 'selector' ? '#' + CSS.escape(cardId) : card.value, {
      documentTitle: '服务确认单 A001',
      noPrint: '.no-print',
      onReady: () => update('打印内容已准备好'),
      onAfterPrint: () => update('打印界面已关闭'),
      onError: error => update('打印失败:' + error.message)
    })
  } catch (error) {
    update('打印失败:' + error.message)
  }
}
onBeforeUnmount(() => { active = false })
</script>

<style scoped>
.print-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.print-actions .el-button { margin-left: 0; }
.print-card { padding: 20px; margin-top: 16px; border: 1px solid var(--el-border-color); border-radius: 8px; }
</style>
参数默认值说明
noPrint.no-print排除打印内容的选择器
documentTitledocument.title打印文档标题
timeout15000等待内容加载的超时毫秒数
onReady()未设置内容准备好
onBeforePrint()未设置打开打印界面前
onAfterPrint()未设置打印流程结束;用户取消也可能触发
onError(error)未设置内容准备失败通知

返回打印实例,不是 Promise;不存在的 DOM 会同步抛错。纸张、缩放和页边距仍由浏览器打印设置控制。详见全局 API。

最后更新:
贡献者: smallwei
Prev
License 授权书
Next
Screenshot 页面截图