当前位置:首页 > VUE

vue实现模板打印

2026-03-08 16:22:04VUE

Vue 实现模板打印

在 Vue 中实现模板打印功能,通常需要结合浏览器的打印 API 和 CSS 打印样式。以下是几种常见的方法:

使用 window.print() 方法

创建一个打印按钮,点击时调用 window.print() 方法触发浏览器打印对话框。

<template>
  <div>
    <button @click="print">打印</button>
    <div id="printArea">
      <!-- 打印内容区域 -->
      <h1>打印标题</h1>
      <p>打印内容...</p>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    print() {
      window.print();
    }
  }
};
</script>

<style>
@media print {
  body * {
    visibility: hidden;
  }
  #printArea, #printArea * {
    visibility: visible;
  }
  #printArea {
    position: absolute;
    left: 0;
    top: 0;
  }
}
</style>

使用 vue-print-nb 插件

vue-print-nb 是一个 Vue 打印插件,使用起来更加方便。

安装插件:

npm install vue-print-nb --save

使用示例:

<template>
  <div>
    <button v-print="printObj">打印</button>
    <div id="printContent">
      <!-- 打印内容 -->
      <h1>打印标题</h1>
      <p>打印内容...</p>
    </div>
  </div>
</template>

<script>
import print from 'vue-print-nb'

export default {
  directives: {
    print
  },
  data() {
    return {
      printObj: {
        id: 'printContent',
        popTitle: '打印标题'
      }
    }
  }
}
</script>

使用 CSS 控制打印样式

通过 CSS 的 @media print 可以控制打印时的样式,隐藏不需要打印的元素。

@media print {
  .no-print {
    display: none;
  }
  .print-only {
    display: block;
  }
}

动态生成打印内容

如果需要动态生成打印内容,可以使用 Vue 的模板和数据绑定。

vue实现模板打印

<template>
  <div>
    <button @click="printReport">打印报表</button>
    <div id="report" ref="report">
      <h1>{{ title }}</h1>
      <table>
        <tr v-for="item in items" :key="item.id">
          <td>{{ item.name }}</td>
          <td>{{ item.value }}</td>
        </tr>
      </table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: '销售报表',
      items: [
        { id: 1, name: '产品A', value: 100 },
        { id: 2, name: '产品B', value: 200 }
      ]
    }
  },
  methods: {
    printReport() {
      const printWindow = window.open('', '', 'width=800,height=600')
      printWindow.document.write('<html><head><title>打印</title></head><body>')
      printWindow.document.write(this.$refs.report.innerHTML)
      printWindow.document.write('</body></html>')
      printWindow.document.close()
      printWindow.focus()
      printWindow.print()
      printWindow.close()
    }
  }
}
</script>

注意事项

  1. 打印样式可能需要多次调试才能达到理想效果
  2. 某些浏览器可能会阻止弹出窗口,需要在用户交互事件中触发打印
  3. 复杂布局在打印时可能需要特殊处理
  4. 分页控制可以使用 CSS 的 page-break-beforepage-break-after 属性

以上方法可以根据具体需求选择使用,简单的打印需求使用 window.print() 配合 CSS 即可,复杂需求可以考虑使用打印插件或动态生成打印内容。

标签: 模板vue
分享给朋友:

相关文章

vue实现sql操作

vue实现sql操作

在Vue中实现SQL操作通常需要结合后端服务或本地数据库技术,因为前端JavaScript无法直接连接SQL数据库。以下是几种常见的实现方式: 通过API与后端交互 Vue前端通过HTTP请求(如a…

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件可…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &l…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…