news 2026/4/23 2:58:25

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/4/23 14:29:43

3分钟学会SqlBeautifier:Sublime Text终极SQL格式化插件

3分钟学会SqlBeautifier&#xff1a;Sublime Text终极SQL格式化插件 【免费下载链接】SqlBeautifier A sublime plugin to format SQL. It supports both sublime 2 and 3. 项目地址: https://gitcode.com/gh_mirrors/sq/SqlBeautifier 在数据库开发和数据分析的日常工作…

作者头像 李华
网站建设 2026/4/23 14:07:50

Qwen-Image专业级图像生成模型上线,支持1024×1024高清输出

Qwen-Image专业级图像生成模型上线&#xff0c;支持10241024高清输出 在创意内容爆发式增长的今天&#xff0c;设计师、广告从业者和数字媒体团队正面临一个共同挑战&#xff1a;如何在极短时间内交付高质量、高还原度的视觉作品&#xff1f;传统的设计流程依赖反复沟通与多轮修…

作者头像 李华
网站建设 2026/4/23 14:27:46

ComfyUI与Consul服务发现集成:动态负载均衡

ComfyUI与Consul服务发现集成&#xff1a;动态负载均衡 在AI生成内容&#xff08;AIGC&#xff09;从实验玩具走向工业级生产系统的今天&#xff0c;企业面临的不再是“能不能出图”&#xff0c;而是“如何稳定、高效、弹性地运行成百上千个AI推理任务”。尤其是在图像与视频批…

作者头像 李华
网站建设 2026/4/23 11:26:44

OCAT:让OpenCore配置从此告别“手动地狱“的智能管家

OCAT&#xff1a;让OpenCore配置从此告别"手动地狱"的智能管家 【免费下载链接】OCAuxiliaryTools Cross-platform GUI management tools for OpenCore&#xff08;OCAT&#xff09; 项目地址: https://gitcode.com/gh_mirrors/oc/OCAuxiliaryTools 还在为复杂…

作者头像 李华
网站建设 2026/4/23 11:38:53

为什么选择开源输入法:保护隐私的终极安全方案

为什么选择开源输入法&#xff1a;保护隐私的终极安全方案 【免费下载链接】openboard 项目地址: https://gitcode.com/gh_mirrors/op/openboard 在数字时代&#xff0c;我们的每一次键盘输入都可能成为数据收集的目标。OpenBoard作为一款完全开源的Android输入法应用&…

作者头像 李华
网站建设 2026/4/23 17:06:17

PopLDdecay连锁不平衡分析:从入门到精通的全方位指南

PopLDdecay连锁不平衡分析&#xff1a;从入门到精通的全方位指南 【免费下载链接】PopLDdecay PopLDdecay: a fast and effective tool for linkage disequilibrium decay analysis based on variant call format(VCF) files 项目地址: https://gitcode.com/gh_mirrors/po/Po…

作者头像 李华