当前位置: 代码网 > it编程>编程语言>Javascript > vue项目纯前端实现的模板打印功能示例代码

vue项目纯前端实现的模板打印功能示例代码

2024年10月28日 Javascript 我要评论
下载一个插件 npm ivue-print-nb --save在main中引入 import print from “vue-print-nb” vue.

下载一个插件 npm i vue-print-nb --save

在main中引入 import print from “vue-print-nb”

                           vue.use(print);

在postcss.config.js里面展示这个数据样式,如果你的项目中没有这个文件

   那就下载一个插件"npm i postcss-px-to-view --save-dev";

module.exports = {
  plugins: {
    autoprefixer: {},
    "postcss-px-to-viewport": {
      viewportwidth: 1920, //  视窗的宽度,对应的是我们设计稿的宽度,移动端一般是750,如果是pc端那就是类似1920这样的尺寸
      // viewportheight: 1344, // 视窗的高度,移动端一般指定1334,也可以不配置
      unitprecision: 3, // 指定`px`转换为视窗单位值的小数位数(很多时候无法整除)
      viewportunit: "vw", // 指定需要转换成的视窗单位,建议使用vw
      selectorblacklist: ['.nocalssvw'], // 指定不转换为视窗单位的类,可以自定义,可以无限添加,建议定义一至两个通用的类名
      exclude: [/printpersone/],
      // proplist:["*", "!font-size"],//能转化为vw的属性列表
      minpixelvalue: 1, // 小于或等于`1px`不转换为视窗单位,你也可以设置为你想要的值
      mediaquery: false // 允许在媒体查询中转换`px`
    }
  }
};

创建一个和“selectorblacklist”里面名字一样的vue,如上:printpersone.vue

父组件    
<template>
    <div>
        <el-dialog title="表" :visible.sync="dialogvisible" width="70%" :before-close="handleclose">
            <el-button type="primary" plain style="margin-bottom:5px;" @click="handleprint">打印</el-button>
            <el-row>
                <el-col :span="12">
                    <div>
                        <table border="1" class="tableone" v-for="(item, index) in datalist" :key="index">
                            <thead>
                                <tr>
                                    <th>姓名</th>
                                    <td>张三</td>
                                    <th>性别</th>
                                    <td>男</td>
                                    <th>出生年月</th>
                                    <td>1985.5.10</td>
                                    <td rowspan="4" style="width: 130px;"></td>
                                </tr>
                                <tr>
                                    <th>民族</th>
                                    <td>汉</td>
                                    <th>籍贯</th>
                                    <td>汉</td>
                                    <th>出生地</th>
                                    <td>山东</td>
                                </tr>
                            </thead>
                        </table>

                    </div>
                </el-col>
            </el-row>
            <!-- 引用那个打印的模板 -->
            <print-person ref="myprintperson" :list="datalist" />

        </el-dialog>
    </div>
</template>
  
<script>
import printperson from './components/printpersone.vue'
export default {
    components: {
        printperson,
    },
    data() {
        return {
            dialogvisible: false,
            datalist: [],
        };
    },
    created() {

    },
    methods: {
        init(datalist) {
            this.dialogvisible = true;
            this.datalist = datalist;
            this.getlist();
        },
        handleclose(done) {
            done();
        },

        // 打印按钮的事件
        handleprint() {
            let refprint = this.$refs['myprintperson']
            refprint && refprint.print()
        },
    }
};
</script>

打印的模板

打印的模板组件
<template>
    <div>
      <button ref="printbtn" class="myprintbtn" v-print="'#myprintdom'"></button>
      <div id="myprintdom" class="nocalssvw">
        <div class="print-warp" style="page-break-before:always;" v-for="(item, ix) in list" :key="ix">
          <table border="1" class="primt-table print-tableone">
            <thead>
              <tr>
                <td style="width: 68px;" class="pt">姓名</td>
                <td style="width: 58px;">张三</td>
                <td style="width: 68px;" class="pt">性别</td>
                <td style="width: 68px;" class="ptw84">男</td>
                <td style="width: 68px;" class="pt">出生年月</td>
                <td style="width: 68px;">1987.5.10</td>
                <td rowspan="4" colspan="2" style="width: 120px;"></td>
              </tr>
              <tr>
                <td class="pt">民族</td>
                <td>汉</td>
                <td class="pt">籍贯</td>
                <td>汉</td>
                <td class="pt">出生地</td>
                <td>山东</td>
              </tr>
            </thead>
          </table>
        </div>
      </div>
    </div>
  </template>
    
  <script>
  export default {
    props: {
      list: {
        type: array,
        default: () => [],
        required: true,
      },
    },
    data() {
      return {
        myprint: {
          id: 'myprintdom',
          extarcss: ''
        }
      }
    },
    methods: {
      print() {
        this.$refs['printbtn'].click();
      }
    },
  }
  </script>

总结 

到此这篇关于vue项目纯前端实现的模板打印功能的文章就介绍到这了,更多相关vue纯前端模板打印功能内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com