Vue 实现打印功能

Vue 实现 print

第一步 安装组件

//安装print-js
npm install vue-print-nb --save

第二步 在 main.js 全局引入

// 引入下载的print
import print from 'vue-print-nb'
// 全局使用
Vue.use(print);

第三步 使用

<template>
  <div>
    <div class="table">
      <p class="title">统计图表</p>
      <el-table :data="tableData" border id="printContainer">
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="happy" label="爱好"></el-table-column>
      </el-table>
    </div>
    <el-button type="primary" v-show="showbutton" v-print="printObj" @click="printButton">打印</el-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [
        {
          name: "王小虎",
          happy: "王小卤虎皮凤爪",
          status: 0
        },
        {
          name: "王小虎",
          happy: "王小卤虎皮凤爪",
          status: 1
        },
        {
          name: "王小虎",
          happy: "王小卤虎皮凤爪",
          status: 0
        },
        {
          name: "王小虎",
          happy: "王小卤虎皮凤爪",
          status: 1
        }
      ],
      showbutton: true, //打印按钮显示
      printObj: {
        printable: "printContainer", // 标签元素id
        type: "html",
        header: "",
        targetStyles: ["*"],
        style: "@page {margin:0mm 10mm};", //控制页眉页尾间距
        extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>'
      }
    };
  },
  mounted() {
    console.log(this.$route.params.userId);
  },
  methods: {
    printButton() {
      this.showbutton = false;
    }
  }
};
</script>
<style scoped>
#printContainer {
  width: 100%;
}
#printContainer .el-table {
  align-content: center;
}
.table {
  display: flex;
  justify-content: center;
  flex-direction: column;
}
.title {
  text-align: center;
  padding: 20px 0px;
}
@page {
  size: auto A4 landscape;
  margin: 3mm;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值