news 2026/7/18 4:43:41

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/7/13 12:29:48

LobeChat插件系统深度解析:如何扩展你的AI应用功能

LobeChat插件系统深度解析&#xff1a;如何扩展你的AI应用功能 在今天&#xff0c;一个AI助手能不能“干活”&#xff0c;已经不再取决于它背后的模型有多大——GPT-4也好&#xff0c;Llama 3也罢&#xff0c;基础的语言能力早已进入平台期。真正拉开差距的&#xff0c;是它能否…

作者头像 李华
网站建设 2026/7/17 4:09:32

3分钟彻底去除视频水印:智能算法让画面瞬间纯净如初

3分钟彻底去除视频水印&#xff1a;智能算法让画面瞬间纯净如初 【免费下载链接】video-watermark-removal Remove simple watermarks from videos with minimal setup 项目地址: https://gitcode.com/gh_mirrors/vi/video-watermark-removal 还在为视频中那些烦人的水印…

作者头像 李华
网站建设 2026/7/17 6:30:31

5分钟完成SQLite到MySQL数据库迁移:终极转换工具指南

5分钟完成SQLite到MySQL数据库迁移&#xff1a;终极转换工具指南 【免费下载链接】sqlite-to-mysql Script to convert and add sqlite3 database into a mysql/mariadb database 项目地址: https://gitcode.com/gh_mirrors/sq/sqlite-to-mysql 在软件开发的生命周期中&…

作者头像 李华
网站建设 2026/7/17 2:06:50

NAT 配置 实验

实验拓扑实验需求1.按照图示配置IP地址&#xff0c;公网地址100.1.1.1/242.私网A通过NAPT&#xff0c;使R1接入到互联网&#xff0c;私网B通过EASY IP&#xff0c;使R3接入到互联网3.私网A配置NAT SERVER把Telnet的Telnet服务发布到公网&#xff0c;使PC2可以访问三、实验思路1…

作者头像 李华
网站建设 2026/7/16 20:15:59

百度网盘秒传终极指南:5分钟掌握全平台文件转存技巧

百度网盘秒传终极指南&#xff1a;5分钟掌握全平台文件转存技巧 【免费下载链接】baidupan-rapidupload 百度网盘秒传链接转存/生成/转换 网页工具 (全平台可用) 项目地址: https://gitcode.com/gh_mirrors/bai/baidupan-rapidupload 还在为百度网盘下载速度而烦恼&…

作者头像 李华
网站建设 2026/7/16 23:35:26

AutoGPT在法律文书起草中的初步尝试:合同模板生成与条款审查

AutoGPT在法律文书起草中的初步尝试&#xff1a;合同模板生成与条款审查 在律师事务所的某个深夜&#xff0c;一位初级律师正对着屏幕逐字校对第三版设备采购合同。同样的“不可抗力”定义反复出现&#xff0c;付款节点被来回调整&#xff0c;而客户催促签署的消息不断弹出——…

作者头像 李华