PrefaceIn most projects, you will encounter online preview of PDF files. The project uses element ui and is implemented using vue-pdf. Install Dependenciesnpm install --save vue-pdf Related parametersParameter introduction:
accomplish<template> <div> <el-row> <el-button @click="onPreview" size="small">Preview</el-button> </el-row> <el-dialog title="Preview Contract Attachment" :visible.sync="viewVisible" center width="60%" @close='closePreview'> <el-row :gutter="20"> <span>Total {{pageCount}} pages, current page {{pdfPage}}</span> <el-button type="text" size="mini" @click.stop="previousPage">Previous page</el-button> <el-button type="text" size="mini" @click.stop="nextPage">Next page</el-button> </el-row> <div> <pdf :src="src" :page="pdfPage" @num-pages="pageCount = $event" @page-loaded="pdfPage = $event" style="display: inline-block; width: 100%"></pdf> </div> </el-dialog> </div> </template> <script> import pdf from 'vue-pdf' import store from '@/store/' export default { components:{ pdf }, data(){ return { viewVisible: false, src: null, pdfPage : 1, pageCount: 0, token: store.getters.access_token, } }, methods:{ onPreview(){ this.src = pdf.createLoadingTask({ url: 'http://localhost:8082/file/demo.pdf', httpHeaders: {Authorization:'Bearer '+ this.token} }); this.src.promise.then(pdf => { this.viewVisible = true; }); }, closePreview(){ this.pdfPage = 1; }, previousPage(){ let p = this.pdfPage p = p > 1 ? p-1 : this.pageCount this.pdfPage = p }, nextPage(){ let p = this.pdfPage p = p < this.pageCount ? p+1 : 1 this.pdfPage = p } } } </script> EffectNote1. URL url is the file address path this.src = pdf.createLoadingTask({ url: 'http://localhost:8082/file/demo.pdf', }); 2. Set the request header You can set parameters such as token through httpHeaders httpHeaders: {Authorization:'Bearer '+ this.token} 3. src This is more important. Many posts on the Internet are like this. this.src.then(pdf => { this.viewVisible = true; }) Will report TypeError: this.src.then is not a function TypeError: this.src.then is not a function at VueComponent.onPreview (index.vue?6ced:241) at click (index.vue?aaff:261) at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854) at VueComponent.invoker (vue.runtime.esm.js?2b0e:2179) at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854) at VueComponent.Vue.$emit (vue.runtime.esm.js?2b0e:3888) at VueComponent.handleClick (element-ui.common.js?5c96:9413) at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854) at HTMLButtonElement.invoker (vue.runtime.esm.js?2b0e:2179) at HTMLButtonElement.original._wrapper (vue.runtime.esm.js?2b0e:6917) The correct answer is this this.src.promise.then(pdf => { this.viewVisible = true; }); The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: How to read the regional information of IP using Nginx and GeoIP module
>>: MySQL Community Server 5.6 installation and configuration tutorial under Windows 8
1. Tools directory file structure [root@www tools...
1. Installation steps for MySQL 8.0.12 version. 1...
1. Create a shell script vim backupdb.sh Create t...
Table of contents definition The role of the curs...
This article shares the specific code of Vue usin...
What is Publish/Subscribe? Let me give you an exa...
illustrate: VMware IOInsight is a tool that helps...
My first server program I'm currently learnin...
Table of contents Install Docker on CentOS 8 1. U...
Table of contents 1. Introduction to Harbor 1. Ha...
This article is translated from the blog Usability...
Table of contents Preface advantage: shortcoming:...
Table of contents Preface 1. Optimistic Locking A...
Table of contents 1. What is multi-instance 2. Pr...
This is the content of React 16. It is not the la...