照抄例子:https://www.cnblogs.com/steamed-twisted-roll/p/9648255.html
1、npm安装
npm install --save vue-pdf
2、页面引入
3、具体实现
<div class="pdf" v-show="fileType === 'pdf'">
<p class="arrow">
<span @click="changePdfPage(0)" class="turn"
:class="{grey: currentPage==1}">Preview</span>
{{currentPage}} / {{pageCount}}
<span @click="changePdfPage(1)" class="turn"
:class="{grey: currentPage==pageCount}">Next</span>
</p>
<pdf :src="pdfSrc" :page="currentPage" @num-pages="pageCount=$event"
@page-loaded="currentPage=$event" @loaded="loadPdfHandler">
</pdf>
</div>
4、method方法
// 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页
changePdfPage (val) {
// console.log(val)
if (val === 0 && this.currentPage > 1) {
this.currentPage--
// console.log(this.currentPage)
}
if (val === 1 && this.currentPage < this.pageCount) {
this.currentPage++
// console.log(this.currentPage)
}
},
// pdf加载时
loadPdfHandler (e) {
this.currentPage = 1 // 加载的时候先加载第一页
},
5、完整代码:
<template>
<div>
<div style="text-align: left;">
<H2>行业资料</H2>
<el-divider>
</el-divider>
<el-container>
<el-aside width="300px" style="border: 1px solid #eee;height: 1000px; background-color: #D3DCE6;">
<div class="myTree">
<el-tree default-expand-all :props="defaultProps" :data="tableData" @node-click="handleNodeClick"></el-tree>
</div>
</el-aside>
<el-container style="border: 1px solid #eee;margin-left: 10px;">
<div class="pdf" v-show="fileType === 'pdf'">
<p class="arrow">
<span :class="{grey: currentPage==1}" @click="changePdfPage(0)" class="turn">Preview</span>
{{currentPage}} / {{pageCount}}
<span :class="{grey: currentPage==pageCount}" @click="changePdfPage(1)" class="turn">Next</span>
</p>
<pdf
:page="currentPage"
:src="pdfSrc"
@loaded="loadPdfHandler"
@num-pages="pageCount=$event"
@page-loaded="currentPage=$event">
</pdf>
</div>
</el-container>
</el-container>
</div>
</div>
</template>
<script>
import pdf from 'vue-pdf'
export default {
name: "IndustryInformation",
components: {pdf},
data(){
return {
currentPage: 0, // pdf文件页码
pageCount: 0, // pdf文件总页数
fileType: 'pdf', // 文件类型
pdfSrc: '', // pdf文件地址
defaultProps: {
children: 'children',
label: 'name'
},
tableData: [{
id: 1,
name: '道路工程资料',
children:[
{
id: 2,
name: '公路工程资料编制概述',
children:[
{
id: 21,
name: '路面工程部分分项划分表',
src: '/1.pdf',
},
{
id: 13,
name: '一般建设项目单位工程划分表',
src: '/2.pdf',
},
{
id: 14,
name: '路基工程部分分项划分表',
src: '/3.pdf',
},
{
id: 33,
name: '桥梁工程部分分项划分表',
src: '/4.pdf',
},
{
id: 34,
name: '隧道工程部分分项划分表',
src: '/5.pdf',
}
]
},
{
id: 3,
name: '公路工程竣工资料',
children:[
{
id: 7,
name: '公路工程竣工文件编排层次',
src: '/5.pdf',
},
{
id: 8,
name: '工程洽商记录表',
src: '/4.pdf',
},
{
id: 9,
name: '工程设计表更、洽商一览表',
src: '/3.pdf',
}
]
}
]}]
}
},
methods:{
// 改变PDF页码,val传过来区分上一页下一页的值,0上一页,1下一页
changePdfPage(val) {
// console.log(val)
if (val === 0 && this.currentPage > 1) {
this.currentPage--
// console.log(this.currentPage)
}
if (val === 1 && this.currentPage < this.pageCount) {
this.currentPage++
// console.log(this.currentPage)
}
},
// pdf加载时
loadPdfHandler(e) {
this.currentPage = 1 // 加载的时候先加载第一页
},
handleNodeClick(data) {
this.pdfSrc = data.src;
}
}
}
</script>
<style scoped>
.myTree /deep/ .el-tree {
position: relative;
cursor: default;
color: #606266;
background-color: #D3DCE6;
}
</style>
6、最终效果
后记:主要是priview和next翻页不是我想要的效果,因此只能另寻它法。
注意:pdf文件需要放在public路径下任意文件夹中,不支持相对路径