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
In the process of web project development, we oft...
Let's take a look at the problem of VScode re...
First, let's talk about the in() query. It is...
Table of contents mysql filtered replication Impl...
Table of contents 1. Introduction 2. Introduction...
This article shares the specific code for WeChat ...
Preface Learn to create a file system on your sys...
transform and translate Transform refers to trans...
I heard that there is an interview question: How ...
Common Nginx configuration allows cross-domain se...
background First of all, I would like to state th...
The following installations all use the ~/ direct...
Preface For a data-centric application, the quali...
Why should we read the log? For example, if the c...
Preface During development, we often encounter va...