integrated_traffic_vue/src/components/pdf/index.vue

54 lines
1.2 KiB
Vue
Raw Normal View History

2024-01-05 17:33:18 +08:00
<template>
<el-dialog title="文档" v-model="visible">
<div v-if="visible">
<vue-pdf
ref="pdfRef"
v-for="page in numOfPages"
:key="page"
2024-01-08 14:31:18 +08:00
:src="VITE_FILE_URL + src"
2024-01-05 17:33:18 +08:00
:page="page"
/>
</div>
</el-dialog>
</template>
<script setup>
import { watchEffect, ref } from "vue";
import { VuePdf, createLoadingTask } from "vue3-pdfjs/esm";
import { ElMessage } from "element-plus";
import { useVModel } from "@vueuse/core";
2024-01-08 14:31:18 +08:00
const VITE_FILE_URL = import.meta.env.VITE_FILE_URL;
2024-01-05 17:33:18 +08:00
defineOptions({
name: "LayoutPdf",
});
const props = defineProps({
src: {
type: String,
required: true,
},
visible: {
type: Boolean,
default: false,
required: true,
},
});
const emits = defineEmits(["update:visible"]);
const visible = useVModel(props, "visible", emits);
const pdfRef = ref(null);
const numOfPages = ref(0);
watchEffect(() => {
if (props.visible) {
2024-01-08 14:31:18 +08:00
const loadingTask = createLoadingTask(VITE_FILE_URL + props.src);
2024-01-05 17:33:18 +08:00
loadingTask.promise
.then((pdf) => {
numOfPages.value = pdf.numPages;
})
.catch(() => {
visible.value = false;
ElMessage.error("文件加载失败");
});
}
});
</script>