您现在的位置是:网站首页>VueVue

vue-pdf的使用说明

左鹏08-16 12:17:43Vue1,315人已围观

简介vue-pdf使用方法如下 1、安装 npm i pdfjs-dist@2.5.207 npm i vue-pdf@4.2.0 2、使用 <template> <div class="drawer-pdf"> <el-drawer class="drawer" :title="title" :visible="show" :direction="direction" size

vue-pdf使用方法如下

1、安装

npm i pdfjs-dist@2.5.207
npm i vue-pdf@4.2.0

2、使用

<template>
  <div class="drawer-pdf">
    <el-drawer
      class="drawer"
      :title="title"
      :visible="show"
      :direction="direction"
      size="50%"
      :before-close="handleClose"
    >
      <el-pagination
        style="text-align: center"
        v-if="pageTotalNum"
        :hide-on-single-page="true"
        background
        layout="prev, pager, next, jumper"
        :page-size="1"
        @current-change="handleCurrentChange"
        :total="pageTotalNum"
        :current-page.sync="pageNum"
      >
      </el-pagination>
      <el-scrollbar style="height: 100%; margin-top: 20px">
        <pdf
          ref="pdf"
          :src="src"
          :page="pageNum"
          @progress="loadedRatio = $event"
          @num-pages="pageTotalNum = $event"
          @error="pdfError"
        >
        </pdf>
      </el-scrollbar>
    </el-drawer>
  </div>
</template>
<script>
import pdf from "vue-pdf";

export default {
  name: "DrawerPdf",
  components: { pdf },
  props: {
    direction: {
      type: String,
      default: "ltr",
    },
  },
  data() {
    return {
      pageNum: 1,
      pageTotalNum: 0,
      loadedRatio: 0,
      src: undefined,
    };
  },
  computed: {
    // 抽屉数据
    drawerPdfDialog() {
      return this.$store.state.home.drawerPdfDialog;
    },
    show() {
      return this.drawerPdfDialog.show;
    },
    pdfUrl() {
      return this.drawerPdfDialog.pdfUrl;
    },
    title() {
      return this.drawerPdfDialog.title;
    },
  },
  watch: {
    show(newVal) {
      if (newVal) {
        if (!this.pdfUrl) {
          this.handleClose();
          this.$message.error("PDF文档不存在");
        } else {
          this.src = pdf.createLoadingTask(this.pdfUrl);
        }
      }
    },
  },
  methods: {
    handleCurrentChange(val) {
      this.pageNum = val;
    },
    handleClose() {
      this.$store.dispatch("home/SetDrawerPdfDialog", {
        show: false,
        pdfUrl: "",
        title: "",
      });
    },
    pdfError(e) {
      console.log(e)
      this.$message.error(`PDF文档打开失败:${e.message} - ${this.pdfUrl}`);
      this.handleClose();
    },
  },
};
</script>
<style lang="scss" scoped>
.drawer-pdf {
  .drawer {
    ::v-deep.el-pagination__jump {
      color: #fff;
    }
    ::v-deep.el-drawer {
      background-color: #2a3e74;
      .el-drawer__body {
        overflow: hidden;
      }
      .el-drawer__header {
        color: #fff;
        text-align: center;
      }
    }
  }
}
</style>

 

文章评论

    请先说点什么
    热门评论
    0人参与,0条评论
    正在载入评论列表...

    相关文章

    本栏推荐

    vue-pdf的使用说明

    站点信息

    • 建站时间:2018-09-18
    • 网站程序:Spring Boot
    • 主题模板:《今夕何夕》
    • 文章统计:104条
    • 微信公众号:扫描二维码,关注我们
    登陆您的账户