In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
这篇文章主要介绍了钉钉小程序web-view怎么内嵌H5页面并实现通信的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇钉钉小程序web-view怎么内嵌H5页面并实现通信文章都会有所收获,下面我们一起来看看吧。
1.引入页面
在管理端新建页面,同时在钉钉页面使用web-view引入,需要后端配合传入合适的token。
钉钉页面引入
2.在H5页面向钉钉发送消息
H5页面使用dd.postMessage()进行消息发送,并使用 dd.navigateTo()进行页面的跳转。
返回并发送消息 export default { data() { return { } }, created() { var userAgent = navigator.userAgent if (userAgent.indexOf('AlipayClient') > -1) { // 支付宝小程序的 JS-SDK 防止 404 需要动态加载,如果不需要兼容支付宝小程序,则无需引用此 JS 文件。 [xss_clean]ln('' + '') }, methods: { handleToDT() { // 网页向小程序 postMessage 消息 dd.postMessage({ name: '测试web-view' }) setTimeout(()=>{ dd.navigateTo({ url: '/pages/index/myNetwork/index' }) },500) }, } },
钉钉页面使用@message进行消息的接受,但很坑的是,文档上接收方法为onMessage,但uniapp中需要改为@message才能接收到消息。
export default { data() { return { webViewContext: '', token: uni.getStorageSync('x-token') } }, onLoad(e){ }, methods: { test(e){ console.log(e) } }, }3.在钉钉页面向H5发送消息,继而实现双向通信
钉钉页面创建实例,并调用this.webViewContext.postMessage()方法发送消息
export default { data() { return { webViewContext: '', token: uni.getStorageSync('x-token') } }, onLoad(e){ this.webViewContext = dd.createWebViewContext('web-view-1'); }, methods: { test(e){ this.webViewContext.postMessage({'sendToWebView': '1'}); } }, }
H5页面在mounted中使用dd.onMessage接收消息
mounted() { // 接收来自小程序的消息。 dd.onMessage = function(e) { console.log(e); //{'sendToWebView': '1'} } },4.注意 内容调试方式
钉钉页面在控制台查看数据即可 H5数据调试控制台开启方式
5.附双向通信全部代码
钉钉页面(即uniapp编写页面)
export default { data() { return { webViewContext: '', token: uni.getStorageSync('x-token') } }, onLoad(e){ this.webViewContext = dd.createWebViewContext('web-view-1'); }, methods: { test(e){ console.log(e) this.webViewContext.postMessage({'sendToWebView': '1'}); } }, }
H5页面(即挂载到管理端页面)
返回并发送消息 export default { data() { return { } }, created() { var userAgent = navigator.userAgent if (userAgent.indexOf('AlipayClient') > -1) { // 支付宝小程序的 JS-SDK 防止 404 需要动态加载,如果不需要兼容支付宝小程序,则无需引用此 JS 文件。 [xss_clean]ln('' + '') } }, mounted() { // 接收来自小程序的消息。 dd.onMessage = function(e) { console.log(e); //{'sendToWebView': '1'} } }, methods: { handleToDT() { // javascript // 网页向小程序 postMessage 消息 dd.postMessage({ name: '测试web-view' }) setTimeout(()=>{ dd.navigateTo({ url: '/pages/index/myNetwork/index' }) },500) }, }}关于"钉钉小程序web-view怎么内嵌H5页面并实现通信"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"钉钉小程序web-view怎么内嵌H5页面并实现通信"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.