template:
<div class="qr-warp-box" v-loading="iLoading">
<iframe :src="qrUrl" style="width: 100%; height: 300px" frameborder="0" scrolling="no" ref="iframe"></iframe>
</div>
script:
// vue生命周期钩子函数 -- 更新之后
updated() {
if (this.$refs.iframe) {
// IE
if (this.$refs.iframe.attachEvent) {
this.$refs.iframe.attachEvent('onload', () => {
// 加载成功
this.iLoading = false;
});
} else {
this.$refs.iframe.onload = () => {
// 加载成功
this.iLoading = false;
};
}
}
},
vue2+elementui完整代码如下:
<template>
<div class="container-warp">
<div class="" v-loading="true">加载的东西</div>
<div class="qr-warp-box" v-loading="iLoading">
<iframe :src="qrUrl" style="width: 100%; height: 300px" frameborder="0" scrolling="no" ref="iframe"></iframe>
</div>
</div>
</template>
<script>
export default {
data() {
return {
iLoading: true,
qrUrl: ''
};
},
created() {
// 模拟异步调用接口
setTimeout(() => {
this.qrUrl = 'https://www.baidu.com';
}, 2000);
},
updated() {
if (this.$refs.iframe) {
// IE
if (this.$refs.iframe.attachEvent) {
this.$refs.iframe.attachEvent('onload', () => {
// 加载成功
this.iLoading = false;
});
} else {
this.$refs.iframe.onload = () => {
// 加载成功
this.iLoading = false;
};
}
}
}
};
</script>
<style lang="scss" scoped>
.container-warp {
width: 100%;
height: 100%;
padding: 20px;
}
</style>

本文介绍如何在Vue项目中使用iframe,并实现对iframe页面加载状态的监测。通过监听iframe的加载完成事件来更新Vue的数据属性,从而控制页面加载指示器的显示与隐藏。
https://blog.youkuaiyun.com/snows_l/article/details/131532067?spm=1001.2014.3001.5501
2588

被折叠的 条评论
为什么被折叠?



