若依vue打印的简单方法

像我们后端程序员做前端的话,有时候真不需要知道什么原理,直接塞就好了
我们选用基于hiprint 的vue-plugin-hiprint来打印
目的是为了实现点击某些行的数据,然后点击某个按钮直接弹出下面的打印
别光看不点赞啊,能解决大问题就回来给我点个赞啊!!!
在这里插入图片描述
此链接 的开源插件大佬是原创,我且拿来使用梳理一下
插件进阶功能请移步: 链接
插件模板制作页面: 链接

首先安装2个依赖

npm install vue-plugin-hiprint
npm i jquery --save-d
npm install vue-plugin-hiprint@0.0.55-beta3
这里是更新包,想获取最新包移步:https://github.com/CcSimple/vue-plugin-hiprint/blob/main/CHANGELOG.md

然后找到项目中的依赖文件夹node_modules/vue-plugin-hiprint/dist/中的print-lock.css文件,复制一份到自己项目的静态资源目录public下
在这里插入图片描述

然后在 public/index.html 中添加:

 <link rel="stylesheet" type="text/css" media="print" href="<%= BASE_URL %>print-lock.css">

在这里插入图片描述

然后 main.js 中引入 依赖

import { hiPrintPlugin } from 'vue-plugin-hiprint'
//引入一维码
import JsBarcode from "jsbarcode";

Vue.use(hiPrintPlugin, '$pluginName')
import jquery from 'jquery'
Vue.prototype.$ = jquery

将下面的代码复制到自己项目中

methods: {
    //打印部分
    // 初始化
    init() {
      // 初始化 打印对象
      const hiprintTemplate = new this.$pluginName.PrintTemplate()
      this.hiprintTemplate = hiprintTemplate
    },
    // 获取打印机列表
    getPrinterList() {
      if (window.hiwebSocket.opened === false) {
        this.$notify.error('打印机客户端未连接')
      }
      // 模板对象获取
      const printerList_ = this.hiprintTemplate.getPrinterList()
      console.info(printerList_)
    },

    // 使用 hiPrintPlugin 控件打印
    confirmPrintPrint(row) {

      // 如果在 main.js 中设置了取消自动连接客户端 是获取不到打印机列表的!!!
      if (window.hiwebSocket.opened === false) {
        this.$notify.error('打印机客户端未连接,请点击右上角头像下载打印客户端')
      }

      // 这一句代码 如果打印出来有问题 可以尝试加进去,没有出现 则不用加
      // 初始化 provider
      this.$pluginName.init({
        providers: [defaultElementTypeProvider()]
      })
      // 这一句代码 如果打印出来有问题 可以尝试加进去,没有出现 则不用加
      this.$pluginName.PrintElementTypeManager.buildByHtml(this.$('.ep-draggable-item'))


      // 清空原内容
      this.$('#printDivXm3').empty()

      // 不使用全局对象, 使用模版打印,在线设计模版:https://ccsimple.gitee.io/vue-plugin-hiprint/
      const hiprintTemplate = new this.$pluginName.PrintTemplate({
        template:

          {"panels":[{"index":0,"name":1,"height":297,"width":210,"paperHeader":49.5,"paperFooter":780,"printElements":[{"options":{"left":17.5,"top":87.5,"height":72,"width":550,"field":"dayincanshu","groupFooterFormatter":"function(t,e){return\"这里自定义统计脚信息\"}","groupFieldsFormatter":"function(type,options,data){ return [\"name\"] }","coordinateSync":false,"widthHeightSync":false,"columns":[[{"width":70,"title":"行号","field":"id","checked":true,"columnId":"id","fixed":false,"rowspan":2,"colspan":1},{"width":100,"title":"人员信息","checked":true,"fixed":false,"rowspan":1,"colspan":2},{"width":100,"title":"销售统计","checked":true,"fixed":false,"rowspan":1,"colspan":2}],[{"width":120,"title":"姓名","field":"name","checked":true,"columnId":"name","fixed":false,"rowspan":1,"colspan":1,"align":"left"},{"width":120,"title":"性别","field":"gender","checked":true,"columnId":"gender","fixed":false,"rowspan":1,"colspan":1},{"width":120,"title":"销售数量","field":"count","checked":true,"columnId":"count","fixed":false,"rowspan":1,"colspan":1},{"width":120,"title":"销售金额","field":"amount","checked":true,"columnId":"amount","fixed":false,"rowspan":1,"colspan":1}]]},"printElementType":{"title":"表格","type":"table","editable":true,"columnDisplayEditable":true,"columnDisplayIndexEditable":true,"columnTitleEditable":true,"columnResizable":true,"columnAlignEditable":true,"isEnableEditField":true,"isEnableContextMenu":true,"isEnableInsertRow":true,"isEnableDeleteRow":true,"isEnableInsertColumn":true,"isEnableDeleteColumn":true,"isEnableMergeCell":true}}],"paperNumberLeft":565.5,"paperNumberTop":819,"paperNumberContinue":true,"watermarkOptions":{"content":"vue-plugin-hiprint","rotate":25,"timestamp":true,"format":"YYYY-MM-DD HH:mm"}}]}


      })

      // 挂载打印内容
      hiprintTemplate.design('#printDivXm3')

      // 打印数据,要和 上面 panel 内的 field 参数一致
      const table = [{
        id: '我是条形码内容',
        name: '我是测试内容1',
        gender: '我是测试内容2'
      },
        {
          id: '我是条形码内容',
          name: '我是测试内容1',
          gender: '我是测试内容2'
        },]
     
      const contentToPrint = document.getElementById("contentToPrint");
        var dayincanshu = {
          // dayincanshu:this.dayincanshu
          dayincanshu:table
        }
        hiprintTemplate.print(dayincanshu)
    },}

注意这个字段名要匹配,就ok 了
在这里插入图片描述
2023.12.26日更新:
发现此插件可能与Raw Loader 加载器起冲突,导致线框无法加载。

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
若依是一个基于Vue3的后台管理系统框架,可以使用若依提供的组件和工具来实现实时报警弹窗。 以下是一个简单的示例: 1. 在若依项目中安装WebSocket: ```bash npm install --save ws ``` 2. 创建一个WebSocket实例: ```javascript import { ref, onMounted } from 'vue' import WebSocket from 'ws' export default { setup() { const alertMsg = ref('') const ws = new WebSocket('ws://localhost:8080') ws.onmessage = (event) => { alertMsg.value = event.data } onMounted(() => { ws.addEventListener('open', () => { console.log('WebSocket connected') }) }) return { alertMsg } } } ``` 3. 在若依中使用Alert组件来显示报警信息: ```html <template> <div> <el-alert v-if="alertMsg" title="Alert" type="error" :closable="false" :description="alertMsg" /> </div> </template> ``` 在上述代码中,我们使用了Element UI提供的Alert组件来显示报警信息。当alertMsg有值时,Alert组件会显示错误类型的提示框,并将alertMsg作为描述信息显示出来。 4. 在服务器端实现WebSocket: ```javascript const WebSocket = require('ws') const wss = new WebSocket.Server({ port: 8080 }) wss.on('connection', (ws) => { console.log('WebSocket connected') ws.send('Welcome to WebSocket server') setInterval(() => { ws.send('Alert!!!') }, 5000) ws.on('close', () => { console.log('WebSocket closed') }) }) ``` 在上述代码中,我们使用WebSocket.Server创建了一个WebSocket服务器,并在客户端连接时发送欢迎消息。然后,我们使用setInterval函数每隔5秒向所有连接的客户端发送报警消息。当客户端关闭连接时,服务器会打印一条消息。 注意:在实际使用中,你需要将上述代码嵌入到若依框架的页面组件中,并根据具体需求进行调整和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值