多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
* [test.nvue](https://www.kancloud.cn/wangking/uniapp/1905752#testnvue_2) * [h5页面](https://www.kancloud.cn/wangking/uniapp/1905752#h5_24) * [官方教程](https://www.kancloud.cn/wangking/uniapp/1905752#_119) ## test.nvue > 如果调用webview的页面使用nvuew才可以实现彼此实时通讯; > nvue 调用 webview 需要手动指定宽高,不像vue页面的 webview是自动铺满,如果不需要进行通讯情况下,可采用vue ~~~ <template> <view> <web-view @onPostMessage="message" :style="{width:'200px', height:'200px'}" src="https://uniapp.dcloud.io/static/web-view.html"></web-view> </view> </template> <script> export default { methods:{ message:function(res){ console.log(res) } } } </script> ~~~ ## h5页面 > 参考[https://uniapp.dcloud.io/static/web-view.html](https://uniapp.dcloud.io/static/web-view.html) ~~~ <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /> <title>网络网页</title> </head> <body> <div class="btn-list"> <button class="btn" type="button" data-action="navigateTo">navigateTo</button> <button class="btn" type="button" data-action="redirectTo">redirectTo</button> <button class="btn" type="button" data-action="navigateBack">navigateBack</button> <button class="btn" type="button" data-action="reLaunch">reLaunch</button> <button class="btn" type="button" data-action="switchTab">switchTab</button> </div> <div class="post-message-section"> <p class="desc">网页向应用发送消息,注意:小程序端应用会在此页面后退时接收到消息。</p> <div class="btn-list"> <button class="btn btn-red" type="button" id="postMessage">postMessage</button> </div> </div> <script type="text/javascript"> var userAgent = navigator.userAgent; if (userAgent.indexOf('AlipayClient') > -1) { // 支付宝小程序的 JS-SDK 防止 404 需要动态加载,如果不需要兼容支付宝小程序,则无需引用此 JS 文件。 document.writeln('<script src="https://appx/web-view.min.js"' + '>' + '<' + '/' + 'script>'); } else if (/QQ/i.test(userAgent) && /miniProgram/i.test(userAgent)) { // QQ 小程序 document.write('<script type="text/javascript" src="https://qqq.gtimg.cn/miniprogram/webview_jssdk/qqjssdk-1.0.0.js"><\/script>'); } else if (/miniProgram/i.test(userAgent) && /micromessenger/i.test(userAgent)) { // 微信小程序 JS-SDK 如果不需要兼容微信小程序,则无需引用此 JS 文件。 document.write('<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"><\/script>'); } else if (/toutiaomicroapp/i.test(userAgent)) { // 头条小程序 JS-SDK 如果不需要兼容头条小程序,则无需引用此 JS 文件。 document.write('<script type="text/javascript" src="https://s3.pstatp.com/toutiao/tmajssdk/jssdk-1.0.1.js"><\/script>'); } else if (/swan/i.test(userAgent)) { // 百度小程序 JS-SDK 如果不需要兼容百度小程序,则无需引用此 JS 文件。 document.write('<script type="text/javascript" src="https://b.bdstatic.com/searchbox/icms/searchbox/js/swan-2.0.18.js"><\/script>'); } else if (/quickapp/i.test(userAgent)) { // quickapp document.write('<script type="text/javascript" src="https://quickapp/jssdk.webview.min.js"><\/script>'); } if (!/toutiaomicroapp/i.test(userAgent)) { document.querySelector('.post-message-section').style.visibility = 'visible'; } </script> <!-- uni 的 SDK --> <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script> <script type="text/javascript"> document.addEventListener('UniAppJSBridgeReady', function() { document.querySelector('.btn-list').addEventListener('click', function(evt) { var target = evt.target; if (target.tagName === 'BUTTON') { var action = target.getAttribute('data-action'); switch (action) { case 'switchTab': uni.switchTab({ url: '/pages/tabBar/API/API' }); break; case 'reLaunch': uni.reLaunch({ url: '/pages/tabBar/component/component' }); break; case 'navigateBack': uni.navigateBack({ delta: 1 }); break; default: uni[action]({ url: '/pages/component/button/button' }); break; } } }); document.getElementById('postMessage').addEventListener('click', function() { uni.postMessage({ data: { action: 'message' } }); }); }); </script> </body> </html> ~~~ ## 官方教程 > [https://uniapp.dcloud.io/component/web-view](https://uniapp.dcloud.io/component/web-view)