更新记录
1.0.0(2025-09-19)
下载此版本
平台兼容性
uni-app
Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
iOS |
鸿蒙 |
√ |
√ |
√ |
√ |
√ |
× |
- |
- |
- |
微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
快应用-华为 |
快应用-联盟 |
× |
× |
× |
× |
× |
× |
- |
× |
× |
- |
- |
其他
使用须知
- 1、该组件利用renderJS渲染h5端的iframe实现,所以只支持h5和APP-VUE。
- 2、本地html最好放在static静态资源文件夹中,这样h5才能访问到。
- 3、本地html访问路径要使用绝对路径,比如/static/html/index.html。
- 4、h5使用时,要保证访问的网页链接和当前h5是同源的,否则很多操作无法执行。
- 6、有什么不懂,可以加 1_0_8_7_7_3_5_9_4_2 去掉横杠即可。
props属性
属性名 |
类型 |
默认值 |
可选值 |
说明 |
src |
String |
---- |
---- |
访问链接 |
sandbox |
String |
allow-same-origin allow-top-navigation allow-forms allow-scripts allow-modals allow-downloads |
---- |
沙盒模式(原生属性) |
allow |
String |
---- |
autoplay,fullscreen,encrypted-media,picture-in-picture |
允许一些操作(原生属性) |
allowfullscreen |
Boolean |
true |
true,false |
允许全屏(原生属性) |
frameborder |
String |
'0' |
---- |
边框(原生属性) |
iframeClass |
String,Object |
---- |
---- |
iframe自定义类 |
iframeStyle |
String,Object |
---- |
---- |
iframe自定义样式 |
loadingShow |
Boolean |
true |
true,false |
展示loading |
overrideUrlLoadingOptions |
Object |
---- |
---- |
拦截配置(仅支持APP)官方文档 |
event事件
事件名 |
参数 |
说明 |
loadstart |
---- |
开始加载事件 |
loaded |
---- |
加载完成事件 |
error |
---- |
加载出错事件 |
message |
---- |
网页向APP通知消息事件 |
backerror |
---- |
返回出错事件(触发该事件表示当前已经是最前面无法返回了) |
forwarderror |
---- |
前进出错事件(触发该事件表示当前已经是最后面无法前进了) |
overrideurlloading |
---- |
拦截事件(禁止APP) |
destroyed |
---- |
iframe销毁通知 |
内置方法
方法名 |
参数 |
说明 |
back |
---- |
返回上一个网页 |
forward |
---- |
前进下一个网页 |
assign |
---- |
调用内部iframe跳转链接 |
showLoading |
---- |
展示进度条 |
hideLoading |
---- |
关闭进度条 |
evalJS |
---- |
注入js |
evalCSS |
---- |
注入css |
setData |
---- |
设置网页中window.变量 |
destroy |
---- |
销毁iframe |
快速开始
<!-- 线上地址 -->
<yb-iframe style="height:200px" src="https://www.baidu.com"></yb-iframe>
<!-- 本地网页 -->
<yb-iframe style="height:200px" src="/static/html/index.html"></yb-iframe>
自定义拦截地址(仅支持APP)
<!-- 线上地址 -->
<yb-iframe ref="iframe" style="height:200px" src="https://www.baidu.com"
:overrideUrlLoadingOptions="overrideUrlLoadingOptions"
@overrideurlloading="handleOverrideurlloading"></yb-iframe>
export default {
data () {
return {
overrideUrlLoadingOptions: {
effect: 'instant',//拦截URL请求生效时机 instant-立即生效 touchstart-用户操作Webview窗口后生效
mode: 'reject',//拦截模式 allow-不满足match属性定义的条件时拦截url跳转 reject-满足match属性定义的提交时拦截url跳转
match: '',//区配是否需要处理的URL请求 支持正则表达式,默认值为对所有URL地址生效(相当于正则表达式“.*”)
exclude: 'none'//排除拦截处理请求类型 none-不排除任何URL请求(即拦截处理所有URL请求) redirect-排除拦截处理301/302跳转的请求(谨慎使用,非a标签的href触发的URL请求可能会误判断为302跳转)
}
}
},
methods: {
handleOverrideurlloading (e) {
if ( e.url.startsWith('http') ) {//http链接
this.$refs.iframe && this.$refs.iframe.assign(e.url)//调用内部iframe跳转
} else {
//非http链接可以在这里做处理,可能是试图访问三方app
console.log('这里是非正常的链接')
}
}
}
}
iframe和网页通信
- uni-app页面
<!-- 线上地址 -->
<yb-iframe ref="iframe" style="height:200px" src="/static/html/index.html" @message="handleMessage"></yb-iframe>
<button @tap="getIframeContent">通知网页获取内容并返回</button>
export default {
methods: {
//要保证在iframe加载完成后调用
getIframeContent () {
this.$refs.iframe.evalJS(`
var title = document.title
var parent = window.parent || window.top
parent.postMessage({title});
`)
},
handleMessage (e) {
if ( e.title ) {//判断一下是否是自己想要的数据
console.log(e.title)
}
}
}
}