vue中使用iframe标签嵌入页面

vue中使用iframe标签嵌入页面,并给iframe的url添加header
接下来,上代码
defaultIframe.vue是模板页面,里面会给url添加header.

<template>
  <div id="app">
    <div class="check-user-book">
      <a-button v-if="userBookSrc" type="primary" @click="checkUserBook">点击查看使用说明书</a-button>
    </div>
    <iframe
      ref="myIframe"
      style="border:none"
      :width="searchTableWidth"
      :height="searchTableHeight"
      src=""
    ></iframe>
    <!-- sandbox="allow-scripts allow-same-origin" -->
    <a-modal
      :visible="showModal"
      :footer="null"
      width="80%"
      @cancel="handleCancel"
    >
      <iframe :src="userBookSrc" frameborder="0" width="100%" height="800px"></iframe>
    </a-modal>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'

export default {
  props: {
    // 接收的src
    reportUrl: {
      type: String,
      default: ''
    },
    // 接收的说明书的url
    userBookSrc: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      searchTableHeight: 0,
      searchTableWidth: 0,
      showModal: false,
    }
  },
  mounted() {
    window.onresize = () => {
      this.widthHeight() // 自适应高宽度
    }
    this.$nextTick(function () {
      this.widthHeight()
    })

    setTimeout(() => {
      var iframe = this.$refs.myIframe;

      let currentDate = new Date().getTime().toString()
      let str1 = currentDate.slice(0, 1)
      let str2 = currentDate.slice(1)
      let randomNum = (Math.random() * 10).toFixed(0)
      let userUser = JSON.parse(localStorage.getItem('userUser'))

      this.populateIframe(iframe, [["Authorization", userUser.name + JSON.parse(localStorage.getItem('Authorization')) || ''], ["Bouid", str1 + '' + (userUser && userUser.Bouid || '')], ["Currenttime", str2 + '' + randomNum]])
    }, 0);
  },
  computed: {
    ...mapGetters('account', ['user']),
  },
  methods: {
    // 宽高度
    widthHeight() {
      this.searchTableHeight = window.innerHeight - 180
      this.searchTableWidth = window.innerWidth - 300
    },
    // 给iframe的url添加header
    populateIframe(iframe, headers) {
      var xhr = new XMLHttpRequest();
      xhr.open("GET", this.reportUrl + '?u_name=' + this.user.name + '&u_id=' + this.user.Bouid);
      // xhr.responseType = "text";
      xhr.setRequestHeader(headers[0][0], headers[0][1])
      xhr.setRequestHeader(headers[1][0], headers[1][1])
      xhr.setRequestHeader(headers[2][0], headers[2][1])
      xhr.onload = function () {
        if (xhr.readyState === xhr.DONE && xhr.status === 200) {
          if (xhr.responseText.includes(': 302') || xhr.responseText.includes(': 300')) {
            iframe.srcdoc = '验证失效, 请重新登录'
          } else {
            iframe.srcdoc = xhr.response;
          }
        }
      };
      xhr.send();
    },
    // 查看使用说明书
    checkUserBook() {
      this.showModal = true
    },
    // 关闭弹窗
    handleCancel() {
      this.showModal = false
    }
  }
}
</script>
<style lang="less" scoped>
.check-user-book {
  display: flex;
  justify-content: center;
  margin-top: 10px;
}
</style>
  

下面是使用它的组件:

<template>
    <div>
        <!-- 163.高级搜索 -->
        <DefaultIframe :reportUrl="url" :userBookSrc="userBookSrc"></DefaultIframe>
    </div>
</template>
<script>
import httpURL from '@/pages/global_URL.vue'
import DefaultIframe from '@/pages/tools/components/DefaultIframe.vue'

export default {
    components: {
        DefaultIframe
    },
    data() {
        return {
            baseUrl: httpURL.httpURL,
            userBookSrc: 'https://doccnxeNdprWG4D9r0eL8hy4vNB',  // 用户使用说明书
        }
    },
    computed: {
        url() {
            return this.baseUrl + '/api/video_search/'
        },
    }
}
</script>

记录一下,以后可能会用到的,加油

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值