最近项目中需求为在浏览器上阅览PDF格式的文件,以前没有写过,随即上网查阅,发现你们常使用的为两个插件。
其一是火狐出品的pdf.js,github地址:https://github.com/mozilla/pd...;
其二是PDFObject,额,不太清楚做者,github地址:https://github.com/pipwerks/P...。
前者功能强大,社区活跃,后者是基于jquery封装出来的插件,要是在vue中混着jquery总感受怪怪的,因此我选择了前者。
又看了一下有没有轮子可用,诶嘿,vue-pdf,github地址:https://github.com/FranckFrei...。看了文档,可取。
首先下载插件(建议先新建一个demo出来跑,直接撸到开发项目中...出什么幺蛾子...)vue
// 我使用的是yarn npm的话 npm install vue-pdf --dev yarn add vue-pdf --dev
而后在vue文件中引入使用,建议新建一个vue文件二次封装node
<template> <div class="pdf-container"> <pdf :src="pdfUrl"/> </div> </template> <script> import pdf from 'vue-pdf' export default { name: 'VuePdf', components: { pdf }, data() { return { // 此处为示例pdf地址 pdfUrl: 'http://image.cache.timepack.cn/nodejs.pdf' } } } </script>
以后就能够愉快的玩耍了。jquery
不过我迁移到公司项目的时候遇到一个坑,引入这个插件的时候就会报错window is not defined,后来查询资料发现这篇文章,问题才得已解决,感谢做者。https://blog.csdn.net/u010745...webpack
只须要在webpack中设置以下git
module.exports = { // 请忽视这无关的代码 output: { globalObject: "this" } // 请忽视这无关的代码 }
以上。github