PDF预览 vue如何通过路由传参,同时获取传过来的参数

本文详细介绍了在 Vue 中进行路由跳转并传递参数的两种方法,包括使用 `router-link` 和 `$router.push`。同时,展示了如何实现 PDF 预览,通过下载 PDF.js 并将其引入项目,利用 iframe 显示 PDF 文件。内容涵盖了配置路由、获取参数及动态加载 PDF 的具体代码示例。
摘要由CSDN通过智能技术生成

vue 路由跳转,路由传参的几种方式

1.配置好路由

 {
        path: '/Interation/CreditPDF', //路径
        name: 'CreditPDF',
        meta: {
            requireAuth: true
        },
        component: () =>
            import ('@/views/credit/CreditPDF'),//这里是文件所在的位置
    },

2. 路由跳转

2.1. router-link

<router-link :to="{ path: '/Interation/CreditPDF', params: { name: 'name', dataObj: data }, query: { name: 'name', dataObj: data } }"> </router-link>

2.2、$router方式跳转

2.2.1.传参和获取参数(1)

 checkReport(){
       const routeUrl = this.$router.resolve({
        path: "/Interation/CreditPDF",//路径
        query: { 
          orderSn:this.report.order,
          reportSn:this.report.report
        }
      });
      window.open(routeUrl.href, "_blank");
    },
this.$router.push({
            path: '/Interation/CreditPDF',            
            query: {
                name: 'name', 
                dataObj: this.msg
           }
        })
this.$router.push('/Interation/CreditPDF?code=123')
 const orderSn = this.$route.query.orderSn
 const reportSn = this.$route.query.reportSn
 console.log('orderSn,reportSn',orderSn,reportSn);

2.2.2 传参和获取参数(2)

this.$router.push({
            name: '/Interation/CreditPDF',            
            params: {
                name: 'name', 
                dataObj: this.msg
           }
        })
this.$router.params.参数名

pdf预览

1.下载

在官网下载pdfjs http://mozilla.github.io/pdf.js/getting_started/#download
在这里插入图片描述

2.放入项目

将解压后的pdf文件夹放在static目录下
在这里插入图片描述

2333.pdf,的路径是/static/2333.pdf
viewer.html的路径是/static/pdf/web/viewer.html

<template>
  <div id="Credit" :style="availHeight">
  </div>
</template>
<script>
export default {
     name: "Credit",
    data() {
      return {
        availHeight:{
          height:'1040px',
          // background:'pink',
        },
      }
    },
    mounted(){
    // this.$route.params
      const orderSn = this.$route.query.orderSn
      const reportSn = this.$route.query.reportSn
      console.log('orderSn,reportSn',orderSn,reportSn);
	this.availHeight.height=window.screen.availHeight
   //  var url = window.location.origin+"/getReport?"+ "order11=${order11}" + "&order22=${order22}"+ "&report=PDF";//测试用的假接口
     var url = window.location.origin+"/static/2333.pdf";
	var pdfUrl = window.location.origin+"/static/pdf/web/viewer.html?file=" + encodeURIComponent(url);
	 $('#Credit').append('<iframe class="prism-player" id="iframe" src='+pdfUrl+' style="height:100%;width:100%" ></iframe>');

    },
// var url = encodeURIComponent(window.location.origin+'/other/202101/dc88623a-74c4-49c4-bc95-7e34d9cf6163.pdf')
// window.open(window.location.origin + window.location.pathname+'static/pdf/web/viewer.html?file='+url)
}
</script>
<style lang="sass" scoped>
</style>

或者

<template>
  <div id="CreditRisksAnalyzePDF" :style="availHeight">

  </div>
</template>
<script>
export default {
  name: "CreditRisksAnalyzePDF",
  data() {
    return {
      availHeight: {
        height: "1040px",
      },
    };
  },
  mounted() {
    let address = "";
    let devType = process.env.NODE_ENV;
    switch (devType) {
      case "Formal":
        address = "http://11.11.11.111:8081";
        break;
      case "New":
        address = "http://11.11.11.111:8081";
        break;
      default:
        address = "http://11.11.11.111:8081";
    }
    console.log("devType", devType);
    const orderSn = this.$route.query.orderSn;
    const reportSn = this.$route.query.reportSn;
    console.log("orderSn,reportSn", orderSn, reportSn);
    this.availHeight.height = window.screen.availHeight;
    // console.log('this.availHeight.height',this.availHeight.height);
    var url =address +"/data/getReport?" +"orderSn=" +orderSn +"&reportSn=" +reportSn +"&reportStyle=PDF";
    // var url = window.location.origin+"/static/2333.pdf";
    var pdfUrl =window.location.origin +window.location.pathname +"static/pdf/web/viewer.html?file=" +encodeURIComponent(url);
    $("#CreditRisksAnalyzePDF").append('<iframe class="prism-player" id="iframe" src=' +pdfUrl +' style="height:100%;width:100%" ></iframe>'
    );
  },
  // var url = encodeURIComponent(window.location.origin+'/other/202101/dc88623a-74c4-49c4-bc95-7e34d9cf6163.pdf')
  // window.open(window.location.origin + window.location.pathname+'static/pdf/web/viewer.html?file='+url)
};
</script>
<style lang="sass" scoped>
</style>

在这里插入图片描述
出错的话试试这个
注释viewer.js的代码:

//    if (fileOrigin !== viewerOrigin) {
//      throw new Error('file origin does not match viewer\'s');
//    }

在这里插入图片描述

仅供学习参考

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值