<!-- 文件预览组件(通过文件名获取文件url) --> <script lang="ts" setup name="FilePreview"> import * as pdfjsLib from 'pdfjs-dist/build/pdf' import 'pdfjs-dist/web/pdf_viewer.css' import { ElLoading } from 'element-plus' import { getPhotoUrl } from '@/api/file' // 打印文件url const props = defineProps({ printFileName: { type: String, }, }) const pdfContainer = ref(null) const pdfUrl = ref('') async function loadPDF(url: string) { const loadingTask = pdfjsLib.getDocument(url) const pdf = await loadingTask.promise // 渲染逻辑(分页、Canvas绘制等) } /** * 初始化 */ function init() { const loading = ElLoading.service({ lock: true, text: '加载中...', background: 'rgba(255, 255, 255, 0.6)', }) getPhotoUrl(props.printFileName!).then((res: any) => { console.log('文件名称', props.printFileName) pdfUrl.value = res.data loadPDF('/document.pdf') console.log('pdfurl', pdfUrl.value) loading.close() }) } watch(() => props.printFileName, (newVal) => { console.log('newVal', newVal) if (newVal) { init() } }, { immediate: true }) </script> <template> <div ref="pdfContainer" /> </template> <style scoped lang="scss"> .a { font-size: 10px; } </style>