- <template>
- <view>
- <web-view src="https://uniapp.dcloud.net.cn/component/web-view.html">web-view>
- view>
- template>
- 导入web-view 的SDK
-
-
- uni.reLaunch({
- url: '/pages/tabBar/component/component'
- });
-
-
- <script type="text/javascript" src="https://unpkg.com/@dcloudio/uni-webview-js@0.0.3/index.js">script>
- <script type="text/javascript">
- // 待触发 `UniAppJSBridgeReady` 事件后,即可调用 uni 的 API。
- H5端与app端通信
- document.addEventListener('UniAppJSBridgeReady', function() {
- uni.postMessage({
- data: {
- action: 'message'
- }
- });
- uni.getEnv(function(res) {
- console.log('当前环境:' + JSON.stringify(res));
- });
-
- 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>
上面的跳转方式可以解决APP嵌入原生H5页面跳转回APP的问题,但是使用uniapp开发的H5就会失效,在uniapp开发H5页面使用 uni.reLaunch 只能跳转到H5项目本地的页面。
通过官网链接下载 web-view.js文件(下载链接,打开链接右键另存为下载链接,),放到项目的static/js文件中
- import App from './App'
- import webView from './static/js/uni.webview.1.5.2.js'
- ....
- // #ifdef APP-PLUS
- uni.webView.postMessage({
- data: {
- action: 'uni-app',
- }
- });
- uni.webView.reLaunch({
- url: '/pages/tabBar/component/component'
- });
区别在于 uniapp开发的H5要在uni前面加上 webView
- 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>
- <style type="text/css">
- .btn {
- display: block;
- margin: 20px auto;
- padding: 5px;
- background-color: #007aff;
- border: 0;
- color: #ffffff;
- height: 40px;
- width: 200px;
- }
-
- .btn-red {
- background-color: #dd524d;
- }
-
- .btn-yellow {
- background-color: #f0ad4e;
- }
-
- .desc {
- padding: 10px;
- color: #999999;
- }
-
- .post-message-section {
- visibility: hidden;
- }
- style>
- head>
- <body>
- <p class="desc">web-view 组件加载网络 html 示例。点击下列按钮,跳转至其它页面。p>
- <div class="btn-list">
- <button class="btn" type="button" data-action="navigateTo">navigateTobutton>
- <button class="btn" type="button" data-action="redirectTo">redirectTobutton>
- <button class="btn" type="button" data-action="navigateBack">navigateBackbutton>
- <button class="btn" type="button" data-action="reLaunch">reLaunchbutton>
- <button class="btn" type="button" data-action="switchTab">switchTabbutton>
- div>
- <div class="post-message-section">
- <p class="desc">网页向应用发送消息,注意:小程序端应用会在此页面后退时接收到消息。p>
- <div class="btn-list">
- <button class="btn btn-red" type="button" id="postMessage">postMessagebutton>
- div>
- div>
- <script type="text/javascript">
- var userAgent = navigator.userAgent;
- if (userAgent.indexOf('AlipayClient') > -1) {
- // 支付宝小程序的 JS-SDK 防止 404 需要动态加载,如果不需要兼容支付宝小程序,则无需引用此 JS 文件。
- document.writeln('