封装代码片段语法 vue2语法

ops/2024/10/17 15:39:41/

关于函数导入

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/ops/124766.html

相关文章

使用频率最高的 opencv 基础绘图操作 - python 实现

以下是 opencv-python 基本操作绘制示例&#xff0c;绘制&#xff1a; 1&#xff09;圆&#xff0c;2&#xff09;矩形&#xff0c;3&#xff09;线段&#xff0c;4&#xff09;文本。 安装 opencv-python pip install opencv-python 在图上绘制圆的操作&#xff0c;示例如…

C语言题目练习2

前面我们知道了单链表的结构及其一些数据操作&#xff0c;今天我们来看看有关于单链表的题目~ 移除链表元素 移除链表元素&#xff1a; https://leetcode.cn/problems/remove-linked-list-elements/description/ 这个题目要求我们删除链表中是指定数据的结点&#xff0c;最终返…

第十五届蓝桥杯C/C++学B组(解)

1.握手问题 解题思路一 数学方法 50个人互相握手 &#xff08;491&#xff09;*49/2 &#xff0c;减去7个人没有互相握手&#xff08;61&#xff09;*6/2 答案&#xff1a;1024 解题思路二 package 十五届;public class Min {public static void main(String[] args) {i…

【GaussDB】产品简介

产品定位 GaussDB 200是一款具备分析及混合负载能力的分布式数据库&#xff0c;支持x86和Kunpeng硬件架构&#xff0c;支持行存储与列存储&#xff0c;提供PB(Petabyte)级数据分析能力、多模分析能力和实时处理能力&#xff0c;用于数据仓库、数据集市、实时分析、实时决策和混…

FFmpeg的简单使用【Windows】--- 简单的视频混合拼接

实现功能 点击【选择文件】按钮在弹出的对话框中选择多个视频&#xff0c;这些视频就是一会将要混剪的视频素材&#xff0c;点击【开始处理】按钮之后就会开始对视频进行处理&#xff0c;处理完毕之后会将处理后的文件路径返回&#xff0c;并在页面展示处理后的视频。 视频所…

《机器学习与数据挖掘综合实践》实训课程教学解决方案

一、引言 随着信息技术的飞速发展&#xff0c;人工智能已成为推动社会进步的重要力量。作为人工智能的核心技术之一&#xff0c;机器学习与数据挖掘在各行各业的应用日益广泛。本方案旨在通过系统的理论教学、丰富的实践案例和先进的实训平台&#xff0c;帮助学生掌握机器学习…

ChatGPT国内中文版镜像网站整理合集(2024/10/06)

一、GPT中文镜像站 ① yixiaai.com 支持GPT4、4o以及o1&#xff0c;支持MJ绘画 ② chat.lify.vip 支持通用全模型&#xff0c;支持文件读取、插件、绘画、AIPPT ③ AI Chat 支持GPT3.5/4&#xff0c;4o以及MJ绘画 1. 什么是镜像站 镜像站&#xff08;Mirror Site&#xff…

【MySQL_JDBC】Day23-Day28 数据库基础、JDBC基础、聊天室3.0

数据库 数据库基本概念 数据库DataBase 定义: 保存数据的仓库就称为数据库 例如 编写一个用户管理系统&#xff0c;可以让用户在我们编写的系统上进行注册等操作&#xff0c;此时就涉及到了保存用户数据的操作&#xff0c;目前我们的做法可以将一个用户信息以一个User对象…