news 2026/9/7 22:19:30

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/9/8 1:00:52

FaceFusion在教育领域的应用:帮助学生理解AI人脸识别原理

FaceFusion在教育领域的应用&#xff1a;帮助学生理解AI人脸识别原理 在人工智能技术日益渗透日常生活的今天&#xff0c;人脸识别早已不再是实验室里的神秘概念。从手机解锁到校园门禁&#xff0c;从考勤打卡到在线考试身份验证&#xff0c;这项技术正以惊人的速度走进我们的…

作者头像 李华
网站建设 2026/9/7 3:57:06

Figma转HTML终极指南:5步实现设计到代码的无缝转换

Figma转HTML终极指南&#xff1a;5步实现设计到代码的无缝转换 【免费下载链接】figma-html Builder.io for Figma: AI generation, export to code, import from web 项目地址: https://gitcode.com/gh_mirrors/fi/figma-html 在当今快节奏的前端开发环境中&#xff0c…

作者头像 李华
网站建设 2026/9/7 3:27:52

ParsecVDD虚拟显示器:5分钟快速掌握多屏工作流

ParsecVDD虚拟显示器&#xff1a;5分钟快速掌握多屏工作流 【免费下载链接】parsec-vdd ✨ Virtual super display, upto 4K 2160p240hz &#x1f60e; 项目地址: https://gitcode.com/gh_mirrors/pa/parsec-vdd 还在为有限的屏幕空间而烦恼吗&#xff1f;想要在单台设备…

作者头像 李华
网站建设 2026/9/7 12:11:36

终极视频水印去除指南:3步轻松实现纯净画面

终极视频水印去除指南&#xff1a;3步轻松实现纯净画面 【免费下载链接】video-watermark-removal Remove simple watermarks from videos with minimal setup 项目地址: https://gitcode.com/gh_mirrors/vi/video-watermark-removal 还在为视频中顽固的水印而烦恼吗&am…

作者头像 李华
网站建设 2026/9/7 23:41:49

Wan2.2-T2V-A14B在开源社区的应用热度分析及前景展望

Wan2.2-T2V-A14B在开源社区的应用热度分析及前景展望 最近&#xff0c;如果你关注AIGC领域的技术动态&#xff0c;可能会注意到一个名字频繁出现在开发者论坛和模型托管平台上——Wan2.2-T2V-A14B。这款由阿里巴巴推出的文本到视频生成&#xff08;Text-to-Video, T2V&#xff…

作者头像 李华
网站建设 2026/9/7 13:30:22

PyLink完整指南:用Python轻松操控SEGGER J-Link进行嵌入式开发

PyLink完整指南&#xff1a;用Python轻松操控SEGGER J-Link进行嵌入式开发 【免费下载链接】pylink Python Library for device debugging/programming via J-Link 项目地址: https://gitcode.com/gh_mirrors/py/pylink 想要在Python环境中无缝集成SEGGER J-Link调试器吗…

作者头像 李华