vue3 print ts打印组件

依赖安装

npm install vue3-print-ts

全局注入

import { createApp } from 'vue'
import App from './App.vue'
import Print from 'vue3-print-ts'

const app = createApp(App)
app.directive("print", Print)

页面使用(element plus ui 例子)

<template>
  <div class="container">
    <el-row :gutter="24">
      <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
        相关文档:
        <a href="https://github.com/huang-long/vue3-print-ts" target="_blank">vue3-print-ts</a>
      </el-col>
    </el-row>

    <el-divider content-position="left">print</el-divider>

    <el-row :gutter="24">
      <el-button type="primary" size="small" icon="Printer" v-print="printObj">打印</el-button>
      <div id="loading" v-show="printLoading"></div>
    </el-row>
    <el-divider content-position="center">打印区域</el-divider>
    <div id="printMe" class="print-area">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="date" label="Date" width="180" />
        <el-table-column prop="name" label="Name" width="180" />
        <el-table-column prop="address" label="Address" />
      </el-table>
    </div>
    <el-divider content-position="center">打印区域</el-divider>
  </div>
</template>

<script lang="ts" setup name="DemoPrint" directives="print">
import { ref } from 'vue';
import type { PrintConf } from 'vue3-print-ts';

const tableData = ref([
  {
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-02',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-04',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    date: '2016-05-01',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
])

let printLoading = ref(true);
let printObj= ref<PrintConf>({
  ids: "printMe",
  printTitle: '打印测试',
  extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
  showBackground: true,
  beforePrint() {
    console.log('打印之前')
  },
  afterPrint() {
    console.log('打印之后')
  }
})
</script>
<style lang="less" scoped>
.print-area {
  max-width: 800px;
}
</style>

文档说明

ParameExplainTypeOptionalValueDefaultValue
idsRange print ID ,required valueStringString[]
standardDocument typeStringString[]html5/loose/strict
extraHead<head></head>Add DOM nodes in the node, example:<meta charset=”UTF-8″>StringString[]
extraCss<link> New CSS style sheet, example: http://www.**.com/aa.cssString
printTitle<title></title> Content of labelString
beforePrintCallback function before calling printing tool, example: calback(event)VoidFunction
afterPrintCallback function after calling printing tool, example: calback(event)VoidFunction
showBackgroundprint page show background styleBooleanfalse

例子源代码:vue-sys-manage-el

vue3-print-ts组件:vue3-print-ts

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2023年12月6日
下一篇 2023年12月6日

相关推荐