vue中使用vue-pdf的方法詳解
需求:簡單說~~有兩個pdf文件需在h5上展示,通過點擊按鈕切換不同文件的顯示
注:
1.vue-pdf默認(rèn)展示首頁,我這里的需求是通過滑動展示所有頁面,這里使用的v-for遍歷。有多少頁就加載了多少個pdf組件。
2.pdf文件存在跨域問題,這個需要后端同學(xué)支持。
3.demo上的pdf文件只有一頁,測試多頁展示,自己改用多頁pdf文件即可
<template>
<div class="pdf_wrap">
<div class="pdf_list">
<!-- src:pdf地址,page: 當(dāng)前顯示頁 -->
<pdf v-for="i in numPages" :key="i" :src="src" :page="i" style="width: 100%" > </pdf>
</div>
<Button type="info" @click="loadPdf(pdfUrl1)">
文件1
</Button>
<Button type="info" native-type="submit" @click="loadPdf(pdfUrl2)">
文件2
</Button>
</div>
</template>
<script>
import pdf from 'vue-pdf'
import { Button } from 'vant'
export default {
components: {
pdf, Button
},
data () {
return {
src: '',
numPages: undefined,
pdfUrl1: 'https://clinic-trial-attachments.oss-cn-beijing.aliyuncs.com/output/demo.pdf/1.pdf',
pdfUrl2: 'https://clinic-trial-attachments.oss-cn-beijing.aliyuncs.com/output/123demo'
}
},
mounted () {
this.loadPdf(this.pdfUrl1)
},
methods: {
loadPdf (url) {
this.src = pdf.createLoadingTask(url)
this.src.promise.then(pdf => {
this.numPages = pdf.numPages // 這里拿到當(dāng)前pdf總頁數(shù)
})
}
}
}
</script>
<style scoped>
.pdf_wrap {
background: #fff;
height: 100vh
}
.pdf_list {
height: 80vh;
overflow: scroll;
}
button {
margin-bottom: 20px;
}
</style>
總結(jié)
到此這篇關(guān)于vue中使用vue-pdf的方法詳解的文章就介紹到這了,更多相關(guān)vue使用vue-pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實力踩坑?當(dāng)前頁push當(dāng)前頁無效的解決
這篇文章主要介紹了vue實力踩坑?當(dāng)前頁push當(dāng)前頁無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
解決VUE打包后與nginx代理出現(xiàn)加載速度超級慢的問題
這篇文章主要介紹了解決VUE打包后與nginx代理出現(xiàn)加載速度超級慢的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
使用vue-draggable-plus實現(xiàn)拖拽排序
最近遇到一個需求,在 Vue3 的一個 H5 頁面當(dāng)中點擊拖拽圖標(biāo)上下拖動 tab 子項,然后點擊保存可以保存最新的 tab 項順序,同事說可以用 vue-draggable-plus 這個庫來實現(xiàn)拖拽,所以本文給大家介紹了如何使用vue-draggable-plus實現(xiàn)拖拽排序,需要的朋友可以參考下2024-01-01

