原文地址:

VUE页面中加载外部HTML_hh3167253066的博客-CSDN博客_vue加载外部html页面

方法1,用iframe
vue页面
<template>
    <button @click="invokeHtmlMethod">调用html种方法</button>
    <div class="iframestyleset">
      <iframe  name = "iframeMap" id="iframeMapViewComponent"  v-bind:src="getPageUrl"
               width="100%" height="100%"
               frameborder="0" scrolling="no" ref="iframeDom"
      ></iframe>
    </div>
</template>

export default {
    data() {
      return {
        getPageUrl: 'static/testMsgWithIframe.html'
      }
    },
    created() {
      // 初始化时为window绑定一个方法
      window['vueDefinedMyProp'] = (receiveParams) => {
        this.receiveParamsFromHtml(receiveParams)
      }
    },
    methods: {
      receiveParamsFromHtml(res) {
        console.log(res)
      },
      invokeHtmlMethod() {
        window.frames['iframeMap'].lodaTable()
      },
  }

html页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<button οnclick="invockIframeMethod()">点击调用iframe</button>
<script>
  function invockIframeMethod() {
    // 是用widow调用vue绑定的vueDefinedMyProp方法
    window.parent['vueDefinedMyProp']('you are Superman!');
  }
  function lodaTable() {
    let num = 10;
    while (num>0){
      console.log(`number : ${num}`);
      num--;
    }
  }
</script>
</body>
</html>

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐