element-ui vue2 iframe 嵌入外链新解

效果如图

实现原理

在路由中通过 props 传值

html 复制代码
  {
    path: '/iframe',
    component: Layout,
    meta: { title: '小助手', icon: 'example' },
    children: [
      {
        path: 'chatglm',
        name: 'chatglm',
        props: {  name: 'chatglm',url: 'https://chatglm.cn' },
        component: () => import('@/views/iframe/common'),
        meta: { title: 'ai-智普清言', icon: 'table' }
      },
]

组件中获取数据 this.$props.url

html 复制代码
<template>
    <div class="iframe-container">
      <iframe :src="src" scrolling="auto" frameborder="0" class="frame" :onload="onloaded()">
      </iframe>
    </div>
  </template>
  
  <script>
  export default {

    props: ['url']  ,

    data() {
      return {
        src: this.$props.url,
        loading: null
      };
    },
    methods: {
      // 获取路径
      resetSrc: function(url) {
        this.src = url
        this.load()
      },
      load: function() {
        this.loading = this.$loading({  
          lock: true,
          text: "loading...",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.5)",
          // fullscreen: false,
          target: document.querySelector("#main-container ")
        })
      },
      onloaded: function() {
        if(this.loading) {
          this.loading.close()
        }
      }
    },
    mounted() {
      this.resetSrc(this.$props.url);
    },
    watch: {
      $route: {
        handler: function(val, oldVal) {
          // 如果是跳转到嵌套页面,切换iframe的url
          this.resetSrc(this.$props.url);
        }
      }
    }
  };
  </script>
  
  <style lang="scss">
  .iframe-container {
    position: absolute;
    top: 0px;
    left: 0px;
    right: 0px;;
    bottom: 0px;
    .frame {
      width: 100%;
      height: 100%;
    }
  }
  </style>
  

参考

src/views/IFrame/IFrame.vue · 朝雨忆轻尘(Louis)/kitty-ui - Gitee.com

Vue 路由组件传参的 8 种方式 - 掘金

相关推荐
e***U8201 天前
React Hooks性能优化
前端·react.js·前端框架
4***R2401 天前
React数据分析
前端·react.js·前端框架
X***E4631 天前
React课程
前端·react.js·前端框架
4***99741 天前
React音频处理案例
前端·react.js·音视频
1***81531 天前
React组件
前端·javascript·react.js
6***3491 天前
Vue混合现实案例
前端·vue.js·mr
p***43481 天前
Vue混合现实开发
前端·vue.js·mr
ArkPppp1 天前
大道至简-Shadcn/ui设计系统初体验(下):Theme与色彩系统实战
前端·前端框架
炒米23331 天前
通义千问Qwen3-Coder模型帮我总结的typescript笔记
前端
__花花世界1 天前
前端日常工作开发技巧汇总
前端·javascript·vue.js