<template>
<div class="aduit-body">
<el-dialog title="查看附件" :visible.sync="auditeDialog" width="90%" center>
<div v-if="renderCom == 'docx'">
<VueOfficeDocx :src="docx" style="height: 100vh; width: 100%;" @rendered="rendered" @errorHandler="errorHandler"/>
</div>
<div v-if="renderCom == 'pdf'">
<VueOfficePdf :src="docx" style="height: 100vh; width: 100%;" @rendered="rendered" @errorHandler="errorHandler"/>
</div>
</el-dialog>
</div>
</template>
<script>// 引入VueOfficeDocx组件
import VueOfficeDocx from "@vue-office/docx";
// 引入相关样式
import "@vue-office/docx/lib/index.css";
// 引入VueOfficeExcel组件
import VueOfficeExcel from "@vue-office/excel";
// 引入相关样式
import "@vue-office/excel/lib/index.css";
// 引入VueOfficePdf组件
import VueOfficePdf from "@vue-office/pdf";
export default {
components: {
VueOfficeDocx,
VueOfficePdf,
VueOfficeExcel
},
name: 'VueOfficeDialog'
data() {
return {
renderCom: '',
auditeDialog: false,
isShowDocx: false,
docx: 'https://501351981.github.io/vue-office/examples/dist/static/test-files/test.docx',
}
},
methods: {
rendered(){
console.log("渲染完成")
},
errorHandler() {
console.log("预览渲染失败,请重试");
},
open (filename) {
this.docx = filename;
// 根据文件格式显示预览组件
const fileExtension = filename.split(".").pop().toLowerCase();
if (fileExtension === "xlsx") {
this.renderCom = "xlsx";
}
if (fileExtension === "docx") {
this.renderCom = "docx";
}
if (fileExtension === "pdf") {
this.renderCom = "pdf";
}
if (fileExtension === "doc") {
this.renderCom = "doc";
const url = "https://view.officeapps.live.com/op/view.aspx?src=" + filename
window.open(url, '_blank');
return
}
this.auditeDialog = true
}
},
}
</script>
<style lang="scss" scoped>
</style>
<VueOfficeDialog ref='VueOfficeDialog'/>
通过this.$refs.VueOfficeDialog.open(url)预览