项目当中附件预览,相信大家都会遇到,下面分享一个PDF预览组件,可直接使用

  • 安装插件
1
npm install --save vue-pdf
1
2
3
4
5
6
7
8
9
<el-dialog
title="附件预览"
:visible.sync="previewPdf"
width="60%"
top="5vh"
append-to-body
>
<pdf ref="pdf" v-for="i in numPages" :key="i" :src="url" :page="i"> </pdf>
</el-dialog>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<script>
import pdf from 'vue-pdf'
export default {
components:{
pdf
},
data(){
return {
previewPdf: false,
url: "http://192.168.2.222/test.pdf",
numPages: null,
}
},
methods: {
getNumPages() {
let loadingTask = pdf.createLoadingTask(this.url);
loadingTask.promise
.then((pdf) => {
this.numPages = pdf.numPages;
})
.catch((err) => {
console.error("pdf 加载失败", err);
});
},
}
</script>