欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue2.0全局組件之pdf詳解

 更新時間:2017年06月26日 11:15:34   作者:云翳1895  
這篇文章主要為大家詳細(xì)紹了vue2.0全局組件之pdf的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

目的:像elementUI那樣注冊全局組件 預(yù)覽pdf文件

技術(shù)支持:使用火狐的pdf.js http://mozilla.github.io/pdf.js/

準(zhǔn)備:新建一個CPdf.vue文件,把火狐demo里面的build里面的pdf.js下載來,并且依賴了elementUI開發(fā)的其實就是用了<el-button>

編寫:

template

<template>
  <div class="cpdf">
   <div class="center">
     <div class="contor">
      <el-button @click="prev">上一頁</el-button>
      <el-button @click="next">下一頁</el-button> &nbsp; &nbsp;
      <span>Page: <span v-text="page_num"></span> / <span v-text="page_count"></span></span>
      &nbsp; &nbsp;
      <el-button @click="addscale" icon="plus"></el-button>
      <el-button @click="minus" icon="minus"></el-button>
      <el-button id="prev" @click="closepdf">關(guān)閉</el-button>
     </div>
     <canvas class="canvasstyle" id="the-canvas"></canvas>
   </div>
  </div>
</template>

js

 import PDFJS from '../../../static/pdf/pdf.js'
  import {
   mapActions,
   mapGetters
  } from 'vuex';
  export default {
   name: 'c-pdf',
   props: ['pdfurl'],
   data() {

     return {
      pdfDoc: null, //pdfjs 生成的對象
      pageNum: 1,//
      pageRendering: false,
      pageNumPending: null,
      scale: 1.2,//放大倍數(shù)
      page_num: 0,//當(dāng)前頁數(shù)
      page_count: 0,//總頁數(shù)
      maxscale: 2,//最大放大倍數(shù)
      minscale: 0.8//最小放大倍數(shù)
     }
   },
   methods: {
     renderPage(num) { //渲染pdf
      let vm = this
      this.pageRendering = true;
      let canvas = document.getElementById('the-canvas')
      // Using promise to fetch the page
      this.pdfDoc.getPage(num).then(function(page) {
        var viewport = page.getViewport(vm.scale);
        //alert(vm.canvas.height)
        canvas.height = viewport.height;
        canvas.width = viewport.width;

        // Render PDF page into canvas context
        var renderContext = {
         canvasContext: vm.ctx,
         viewport: viewport
        };
        var renderTask = page.render(renderContext);

        // Wait for rendering to finish
        renderTask.promise.then(function() {
         vm.pageRendering = false;
         if(vm.pageNumPending !== null) {
           // New page rendering is pending
           vm.renderPage(vm.pageNumPending);
           vm.pageNumPending = null;
         }
        });
      });
      vm.page_num = vm.pageNum;

     },
     addscale() {//放大
      if(this.scale >= this.maxscale) {
        return
      }
      this.scale += 0.1;
      this.queueRenderPage(this.pageNum)
     },
     minus() {//縮小
      if(this.scale <= this.minscale) {
        return
      }
      this.scale -= 0.1;
      this.queueRenderPage(this.pageNum)
     },
     prev() {//上一頁
      let vm = this
      if(vm.pageNum <= 1) {
        return;
      }
      vm.pageNum--;
      vm.queueRenderPage(vm.pageNum);
     },
     next() {//下一頁
      let vm = this
      if(vm.pageNum >= vm.page_count) {
        return;
      }
      vm.pageNum++;
      vm.queueRenderPage(vm.pageNum);
     },
     closepdf() {//關(guān)閉PDF
      this.$emit('closepdf')
     },
     queueRenderPage(num) {
      if(this.pageRendering) {
        this.pageNumPending = num;
      } else {
        this.renderPage(num);
      }
     }
   },
   computed: {
     ctx() {
      let id = document.getElementById('the-canvas')
      return id.getContext('2d');
     }
   },
   mounted() {
     let vm = this
     PDFJS.getDocument(vm.pdfurl).then(function(pdfDoc_) { //初始化pdf
      vm.pdfDoc = pdfDoc_;
      vm.page_count = vm.pdfDoc.numPages
      vm.renderPage(vm.pageNum);
     });
   }
  }

style less

 .cpdf {
   position: fixed;
   top: 0;
   left: 0;
   background-color: rgba(0, 0, 0, .5);
   width: 100%;
   height: 100%;
   z-index: 99999;
   display: flex;
   justify-content: center;
   align-items: center;
   .center {
     text-align: center;
     height: 100%;
     overflow: auto;
     padding-top: 20px;
     .contor {
      margin-bottom: 10px;
     }
   }
  }

注冊到全局:在main.js 引入CPdf.vue

Vue.component(CPdf.name, CPdf)

使用:在想預(yù)覽pdf文件的組件里面

<c-pdf @closepdf="closepdf" v-show="isshowpdf" :pdfurl="testpdfurl"></c-pdf>
data() {
     return {
      
      isshowpdf:false,
      testpdfurl:'//cdn.mozilla.net/pdfjs/tracemonkey.pdf'
     }
   },
methods: {

     closepdf(){
      this.isshowpdf=false
     },
}

效果:

 

npm :有人關(guān)注 那么我就發(fā)到 npm 上。 地址 https://www.npmjs.com/package/vueshowpdf

本文已被整理到了《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。

關(guān)于vue.js組件的教程,請大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue.extend實現(xiàn)alert模態(tài)框彈窗組件

    vue.extend實現(xiàn)alert模態(tài)框彈窗組件

    這篇文章主要為大家詳細(xì)介紹了vue.extend實現(xiàn)alert模態(tài)框彈窗組件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • vue使用axios的小技巧分享

    vue使用axios的小技巧分享

    這篇文章主要為大家詳細(xì)介紹了一些vue使用axios的小技巧,文中的示例代碼講解詳細(xì),對我們深入掌握vue有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • Element Dropdown下拉菜單的使用方法

    Element Dropdown下拉菜單的使用方法

    這篇文章主要介紹了Element Dropdown下拉菜單的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實例

    使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實例

    今天小編就為大家分享一篇使用vue根據(jù)狀態(tài)添加列表數(shù)據(jù)和刪除列表數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • Vue使用v-viewer插件實現(xiàn)圖片預(yù)覽和縮放和旋轉(zhuǎn)等功能(推薦)

    Vue使用v-viewer插件實現(xiàn)圖片預(yù)覽和縮放和旋轉(zhuǎn)等功能(推薦)

    v-viewer是一個基于viewerjs封裝的vue圖片預(yù)覽組件,有預(yù)覽縮放拉伸旋轉(zhuǎn)切換拖拽等功能,支持配置化,這篇文章主要介紹了Vue使用v-viewer插件實現(xiàn)圖片預(yù)覽和縮放和旋轉(zhuǎn)等功能,需要的朋友可以參考下
    2023-02-02
  • Vue之beforeEach非登錄不能訪問的實現(xiàn)(代碼親測)

    Vue之beforeEach非登錄不能訪問的實現(xiàn)(代碼親測)

    這篇文章主要介紹了Vue之beforeEach非登錄不能訪問的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Vue中的directive指令快速使用

    Vue中的directive指令快速使用

    這篇文章主要介紹了Vue中的directive指令快速使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 如何在 Vue 中使用 JSX

    如何在 Vue 中使用 JSX

    這篇文章主要介紹了如何在 Vue 中使用 JSX,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2021-02-02
  • Vue中使用ElementUI使用第三方圖標(biāo)庫iconfont的示例

    Vue中使用ElementUI使用第三方圖標(biāo)庫iconfont的示例

    這篇文章主要介紹了Vue中使用ElementUI使用第三方圖標(biāo)庫iconfont的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • 前端Vue?select下拉框使用以及監(jiān)聽事件詳解

    前端Vue?select下拉框使用以及監(jiān)聽事件詳解

    由于前端項目使用的是Vue.js和bootstrap整合開發(fā),中間用到了select下拉框,這篇文章主要給大家介紹了關(guān)于前端Vue?select下拉框使用以及監(jiān)聽事件的相關(guān)資料,需要的朋友可以參考下
    2024-03-03

最新評論