news 2026/2/10 18:54:54

Vue3+TypeScript安装及使用vue-plugin-hiprint

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue3+TypeScript安装及使用vue-plugin-hiprint

https://github.com/CcSimple/vue-plugin-hiprint

1、安装

npm install vue-plugin-hiprint

2、手动声明类型

在 src 或 src/types(项目结构有这个 types 目录)目录下,新建 vue-plugin-hiprint.d.ts

declare module "vue-plugin-hiprint" { // 1. 默认导出(在 main.ts 中通过 app.use 使用的对象) const hiprint: { init: (config?: InitConfig) => void; setConfig: (config: PrintConfig) => void; PrintTemplate: typeof PrintTemplate; PrintDesigner: typeof PrintDesigner; parse: (templateJson: string) => any; // 根据模板JSON生成可打印对象 // 其他你可能会用到的顶层API可以继续添加 }; export default hiprint; // 2. 初始化配置类型 interface InitConfig { providers?: any[]; optionItems?: any[]; fontList?: string[]; // 可根据实际需要补充 } // 3. 打印配置类型(对应 hiprint.setConfig) interface PrintConfig { language?: string; printing?: boolean; dpi?: number; preview?: boolean; paper?: PaperConfig; font?: FontConfig; } interface PaperConfig { size?: string; // 如 'A4' width?: number; height?: number; margin?: MarginConfig; } interface MarginConfig { top?: number | string; right?: number | string; bottom?: number | string; left?: number | string; } interface FontConfig { family?: string; size?: number; } // 4. 打印模板类(对应 new hiprint.PrintTemplate()) declare class PrintTemplate { constructor(options?: any); getJson(): string; // 获取模板JSON print(data: any, options?: PrintOptions): void; // 执行打印 print2(container: HTMLElement, data: any, options?: PrintOptions): void; // 在指定容器内预览 updateOption(key: string, value: any): void; addPanel(panel: PanelOption): void; // 根据实际API文档补充更多方法 } interface PrintOptions { title?: string; styleHandler?: () => string; callback?: () => void; } interface PanelOption { index: number; height: number | string; width?: number | string; elements: ElementOption[]; } interface ElementOption { type: string; // 'text', 'table', 'image', 'rect', 'html' 等 title?: string; field?: string; // 数据绑定字段 options: ElementStyleOptions; } interface ElementStyleOptions { width: number | string; height: number | string; left?: number; top?: number; fontSize?: number; fontWeight?: string; textAlign?: "left" | "center" | "right"; color?: string; borderWidth?: number; borderColor?: string; borderStyle?: string; // 更多样式选项... } // 5. 设计器类(对应 new hiprint.PrintDesigner()) declare class PrintDesigner { constructor(options: DesignerOptions); destroy(): void; addElement(element: ElementOption): void; // 根据实际API文档补充更多方法 } interface DesignerOptions { el: string; // 如 '#designer' template?: any; // 模板JSON对象 onCreated?: (template: any) => void; onUpdated?: (template: any) => void; } }

需确保 tsconfig.json 或 tsconfig.app.json 的 include 的设置为:

"include": ["env.d.ts", "src/**/*", "src/**/*.vue"],

tsconfig.json

{ "files": [], "references": [ { "path": "./tsconfig.node.json" }, { "path": "./tsconfig.app.json" }, ], "compilerOptions": { // 全局引入element-plus,如果使用 Volar,在 tsconfig.json 中通过 compilerOptions.type 指定全局组件类型 "types": ["element-plus/global"], "paths": { "@/*": ["./src/*"], "@api/*": ["./src/api/*"], "@components/*": ["./src/components/*"], "@assets/*": ["./src/assets/*"], "@hooks/*": ["./src/hooks/*"], "@router/*": ["./src/router/*"], "@stores/*": ["./src/stores/*"], "@types/*": ["./src/types/*"], "@utils/*": ["./src/utils/*"], "@views/*": ["./src/views/*"], "@api": ["./src/api/index"], // 关键:添加index文件索引 "@components": ["./src/components/index"], // 关键:添加index文件索引 "@hooks": ["./src/hooks/index"], // 关键:添加index文件索引 "@router": ["./src/router/index"], // 关键:添加index文件索引 "@stores": ["./src/stores/index"], // 关键:添加index文件索引 "@types": ["./src/types/index"], // 关键:添加index文件索引 "@utils": ["./src/utils/index"], // 关键:添加index文件索引 } } }

tsconfig.app.json

{ "extends": "@vue/tsconfig/tsconfig.dom.json", "include": ["env.d.ts", "src/**/*", "src/**/*.vue"], "exclude": ["src/**/__tests__/*"], "compilerOptions": { "composite": true, "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo", "baseUrl": ".", "paths": { "@/*": ["./src/*"], "@api/*": ["./src/api/*"], "@components/*": ["./src/components/*"], "@assets/*": ["./src/assets/*"], "@hooks/*": ["./src/hooks/*"], "@router/*": ["./src/router/*"], "@stores/*": ["./src/stores/*"], "@types/*": ["./src/types/*"], "@utils/*": ["./src/utils/*"], "@views/*": ["./src/views/*"], "@api": ["./src/api/index"], // 关键:添加index文件索引 "@components": ["./src/components/index"], // 关键:添加index文件索引 "@hooks": ["./src/hooks/index"], // 关键:添加index文件索引 "@router": ["./src/router/index"], // 关键:添加index文件索引 "@stores": ["./src/stores/index"], // 关键:添加index文件索引 "@types": ["./src/types/index"], // 关键:添加index文件索引 "@utils": ["./src/utils/index"], // 关键:添加index文件索引 } } }

tsconfig.json是Vite项目使用的“Project References”配置,主要的编译配置分散在tsconfig.app.jsontsconfig.node.json中。

如果include字段已经包含类似"src/**/*.ts""src/**/*",那通常已经足够。

否则:

修改tsconfig.app.json中的"include"字段,确保它覆盖src/types目录。通常默认配置已经包含,但请检查确认:

json

{ "compilerOptions": { // ... 其他编译器选项 }, // 确保 include 包含以下模式 "include": [ "src/**/*.ts", "src/**/*.d.ts", // 包含所有 .d.ts 文件 "src/**/*.vue" ] }

3、导入

import hiprint from "vue-plugin-hiprint";
<!--【必须】在index.html 文件中添加打印所需样式(cdn可能不稳定):--> <link rel="stylesheet" type="text/css" media="print" href="https://npmmirror.com/package/vue-plugin-hiprint/files/dist/print-lock.css" /> <!-- OR --> <link rel="stylesheet" type="text/css" media="print" href="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css" /> <!-- 可以调整成 相对链接/自有链接, 【重要】名称需要一致 【print-lock.css】--> <link rel="stylesheet" type="text/css" media="print" href="/print-lock.css" />
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/7 0:52:28

OpenModScan:零基础掌握的Modbus测试神器,让工业通讯调试事半功倍

还在为复杂的Modbus设备调试而头疼吗&#xff1f;面对通讯故障无从下手&#xff1f;OpenModScan作为一款专业的开源Modbus主站工具&#xff0c;正是为你量身打造的工业自动化调试利器。无论你是自动化工程师、系统集成商&#xff0c;还是设备维护人员&#xff0c;这款工具都能让…

作者头像 李华
网站建设 2026/2/2 23:15:57

Solaar完全指南:Linux下罗技设备的终极管理解决方案

Solaar完全指南&#xff1a;Linux下罗技设备的终极管理解决方案 【免费下载链接】Solaar Linux device manager for Logitech devices 项目地址: https://gitcode.com/gh_mirrors/so/Solaar 你是否曾在Linux系统中为罗技设备的配对、电量监控和按键自定义而烦恼&#xf…

作者头像 李华
网站建设 2026/2/10 9:29:12

Transformer架构深度优化:Qwen-Image-Edit-2509性能提升揭秘

Transformer架构深度优化&#xff1a;Qwen-Image-Edit-2509性能提升揭秘 在电商运营的深夜&#xff0c;设计师正为上百张商品图逐一修改价格标签而焦头烂额&#xff1b;社交媒体团队为了适配不同市场的文案&#xff0c;不得不重复制作数十版视觉素材。这些高频、琐碎但关键的任…

作者头像 李华
网站建设 2026/2/9 6:43:42

如何快速掌握Playnite:终极游戏库管理器的完整指南

作为一款强大的开源游戏库管理器&#xff0c;Playnite让您告别多个游戏平台的混乱管理&#xff0c;实现真正的一站式游戏体验。无论您是拥有数十款游戏的轻度玩家&#xff0c;还是收藏数百款游戏的硬核玩家&#xff0c;这款工具都能为您提供高效统一的游戏管理解决方案。 【免费…

作者头像 李华
网站建设 2026/2/2 23:16:04

如何用PyFluent实现CFD仿真全流程自动化?终极Python接口实战指南

如何用PyFluent实现CFD仿真全流程自动化&#xff1f;终极Python接口实战指南 【免费下载链接】pyfluent 项目地址: https://gitcode.com/gh_mirrors/pyf/pyfluent PyFluent作为Ansys Fluent的Python接口库&#xff0c;为工程仿真领域带来了革命性的变革。通过Pythonic的…

作者头像 李华
网站建设 2026/2/7 11:25:25

从GitHub下载EmotiVoice镜像后如何快速启动本地TTS服务

从GitHub下载EmotiVoice镜像后如何快速启动本地TTS服务 在智能语音应用日益普及的今天&#xff0c;越来越多开发者希望构建具备情感表达能力、支持个性化音色的本地化文本转语音&#xff08;TTS&#xff09;系统。然而&#xff0c;主流云服务往往存在延迟高、费用贵、隐私泄露风…

作者头像 李华