封装代码片段语法 vue2语法

news/2024/10/12 18:50:23/

关于函数导入

1.在untils写一个pdfService.js 关于pdf预览和下载的方法

export const previewPdf = async (record) => {const pdfUrln = record.url; // 直接使用 PDF 文件的 URL// const pdfUrln = indexConfig.VITE_GLOB_VIEW_URL + 'static/pdf/web/viewer.html?file=' + record.url;uni.downloadFile({url: pdfUrln,success: function(res) {if (res.statusCode === 200) {console.log('文件下载成功', res);let filePath = res.tempFilePath; // 确认文件路径uni.openDocument({filePath: filePath,fileType: 'pdf', // 明确指定为pdf文件success: function(res) {console.log("文档打开成功");},fail: function(res) {console.error("文档打开失败", res);}});} else {console.error('文件下载失败,状态码:', res.statusCode);}},fail: function(err) {console.error('文件下载失败', err);}});
};export const downloadPdf = async (record) => {uni.showLoading({title: '文件下载中...',});// 使用微信小程序专用 APIwx.downloadFile({url: record.url, // 这里需要是 HTTPS 地址success(res) {if (res.statusCode === 200) {console.log('文件下载成功,文件路径:', res.tempFilePath);uni.showModal({title: '提示',content: '文件下载成功,是否打开?',success: function(modalRes) {if (modalRes.confirm) {wx.openDocument({filePath: res.tempFilePath,showMenu: true, // 支持右上角菜单fileType: 'pdf', // 指定文件类型为 pdfsuccess() {console.log('文档打开成功');},fail(err) {console.log('打开文档失败', err);}});}}});} else {console.log('文件下载失败,状态码:', res.statusCode);}},fail(err) {uni.hideLoading();console.log('文件下载失败', err);},complete() {uni.hideLoading();}});
};

2.引用

 import { 	previewPdf,downloadPdf } from '@/utils/pdfService';
<u-modal :show="showFile" :title="getTitle" @confirm="confirm" @cancel="cancel" :asyncClose="true":showCancelButton="true" :closeOnClickOverlay="true" width="590rpx"><view class="slot-content"><uni-table ref="table" border stripe emptyText="暂无更多数据"><!-- 表头行 --><uni-tr class="custom-header"><uni-th align="center">附件名称</uni-th><uni-th align="center">操作</uni-th></uni-tr><!-- 表格数据行 --><uni-tr v-for="(item, index) in fileList" :key="index"><uni-td align="center">{{ item.fileName }}</uni-td><uni-td width="300rpx" align="center"><a @click="previewPdf(item)">预览</a><a @click="downloadPdf(item)">下载</a></uni-td></uni-tr></uni-table></view></u-modal>

问题来了  报错 说我没注册  也就是说在methods里面 还要写函数

previewPdf(record) {previewPdf(record)},
downloadPdf(record) {downloadPdf(record)},

这样子要写两遍 太麻烦了 

于是有

第二种

在utils里面新建目录mixin 封装,组件中通过 Mixin 的方式使用,而不是每次都手动导入和定义方法

// utils/PdfMixin.js
export const PdfMixin = {methods: {previewPdf(record) {const pdfUrl = record.url;uni.downloadFile({url: pdfUrl,success: function (res) {if (res.statusCode === 200) {let filePath = res.tempFilePath;uni.openDocument({filePath: filePath,fileType: 'pdf',success: function () {console.log("文档打开成功");},fail: function (res) {console.error("文档打开失败", res);},});} else {console.error("文件下载失败,状态码:", res.statusCode);}},fail: function (err) {console.error("文件下载失败", err);},});},downloadPdf(record) {uni.showLoading({title: "文件下载中...",});wx.downloadFile({url: record.url,success(res) {if (res.statusCode === 200) {uni.showModal({title: "提示",content: "文件下载成功,是否打开?",success: function (modalRes) {if (modalRes.confirm) {wx.openDocument({filePath: res.tempFilePath,showMenu: true,fileType: 'pdf',success() {console.log("文档打开成功");},fail(err) {console.log("打开文档失败", err);},});}},});} else {console.log("文件下载失败,状态码:", res.statusCode);}},fail(err) {uni.hideLoading();console.log("文件下载失败", err);},complete() {uni.hideLoading();},});},},
};

引入

	import {PdfMixin} from '@/utils/mixin/pdfMixin';

注册

mixins: [PdfMixin], // 引入 Mixin

可以了


http://www.ppmy.cn/news/1537976.html

相关文章

桂林自闭症寄宿学校:用关爱点亮未来

在繁华的广州&#xff0c;隐藏着一片宁静而充满爱的天地——星贝育园自闭症儿童寄宿制学校。这里&#xff0c;不仅是一所学府&#xff0c;更是一个心灵的港湾&#xff0c;为自闭症儿童提供了一个安全、包容、充满希望的成长环境。自闭症&#xff0c;这个看似遥远却与我们息息相…

Github 2024-10-09 C开源项目日报 Top9

根据Github Trendings的统计,今日(2024-10-09统计)共有9个项目上榜。根据开发语言中项目的数量,汇总情况如下: 开发语言项目数量C项目9PLpgSQL项目1开源时间序列SQL数据库:PostgreSQL扩展 创建周期:2503 天开发语言:C协议类型:OtherStar数量:15982 个Fork数量:838 次关…

ansible 剧本模式

目录 1.剧本格式 ​编辑​编辑2.案例1创建目录分发文件剧本 2.1剧本中用到的命令 2.2书写具体剧本 3.案例2 分发 安装软件包 启动服务的剧本 3.1下载软件包 3.2用yum安装 3.3启动服务 4.找出ansible中对应的模块 5.剧本实现 4.ansible 剧本变量 4.1常用的…

Oracle EBS中 薪资管理 模块的财务流程概览

Oracle E-Business Suite (EBS) 中的薪资管理模块&#xff08;Oracle Payroll&#xff09;是企业资源规划(ERP)系统中一个关键部分&#xff0c;它负责处理员工的薪酬计算、支付以及相关的财务事务&#xff0c;帮助企业快速调整员工薪资并提高薪资管理效率。以下是薪资管理模块的…

【Java】 —— 数据结构与集合源码:Vector、LinkedList在JDK8中的源码剖析

目录 7.2.4 Vector部分源码分析 7.3 链表LinkedList 7.3.1 链表与动态数组的区别 7.3.2 LinkedList源码分析 启示与开发建议 7.2.4 Vector部分源码分析 jdk1.8.0_271中&#xff1a; //属性 protected Object[] elementData; protected int elementCount;//构造器 public …

云原生周刊:Docker大涨价|2024.10.8

开源项目推荐 Kubeshark 如果把 K8s 比作操作系统&#xff0c;那它就是 K8s 上的 tcpdump&#xff0c;使用起来就像 Chrome 开发者工具一样简单直接&#xff0c;能够让 K8s 上微服务之间的网络通信一览无遗。 Teleport 这是一个专为基础设施提供连接、身份验证、访问控制和…

PyCharm打开及配置现有工程(详细图解)

本文详细介绍了如何利用Pycharm打开一个现有的工程&#xff0c;其中包括编译器的配置。 PyCharm打开及配置现有工程 1、打开工程2、配置编译器 1、打开工程 双击PyCharm软件&#xff0c;点击左上角 文件 >> 打开(O)… 选中想要打开的项目之后点击“确定” 2、配置编译器…

【计算机方向】三本计算机视觉IEEE系列,发文量高,影响因子呈上升趋势,备受国人追捧!

本期将为您带来三本计算机SCI 妥妥毕业神刊&#xff01; IEEE Transactions on Pattern Analysis and Machine Intelligence IEEE Transactions on Knowledge and Data Engineering IEEE Transactions on Cognitive and Developmental Systems 期刊名称&#xff1a;IEEE Tr…