uniapp引入uniapp打包的H5跳转H5以及H5返回app方法

ops/2024/10/8 14:16:28/

  1. 在app项目添加webview文件夹添加gridWebview.vue文件。代码如下
<template><view><web-view :src="src" @message="getMessage" ></web-view></view>
</template><script>export default {data() {return {src: '',}},computed: {token() {return this.$store.state.login.token || ''},roles() {return this.$store.state.login.roles || ''},userInfo(){return this.$store.state.login.userInfo || ''}},onLoad(opt) {console.log(opt)//拼接所需参数this.title = opt.titlethis.src = opt.src+ (opt.src.includes('?') ? '&' : '?') + (this.token.length>0 ? 'token=' + this.token : '') + (this.userInfo.phone.length>0 ?`&phone=${this.userInfo.phone}` :'' )+ (this.userInfo.userId.length>0 ?`&userId=${this.userInfo.userId}` :'' )+ (this.userInfo.username.length>0 ?`&username=${this.userInfo.username}` :'' )+ (opt.data?`&data=${opt.data}` :'' )+ (opt.cityid?`&cityid=${opt.cityid}` :'' )},onShow() {// #ifdef APP-PLUS设置webview显示时顶部丢失问题var height = 0; //定义动态的高度变量,如高度为定值,可以直接写uni.getSystemInfo({//成功获取的回调函数,返回值为系统信息success: (sysinfo) => {height = sysinfo.windowHeight -40; //自行修改,自己需要的高度 此处如底部有其他内容,可以直接---(-50)这种,如果不减,页面会下沉,看不到底部导航栏},complete: () => {}});var currentWebview = this.$scope.$getAppWebview(); //获取当前web-viewsetTimeout(function() {var wv = currentWebview.children()[0];wv.setStyle({ //设置web-view距离顶部的距离以及自己的高度,单位为pxtop: 40, //此处是距离顶部的高度,应该是你页面的头部height: height, //webview的高度scalable: false, //webview的页面是否可以缩放,双指放大缩小,})}, 500); //如页面初始化调用需要写延迟// #endif},methods: {customBack() {// 在Webview页面中调用uni.navigateBack()  uni.navigateBack();},getMessage(event) {  //在H5端使用通信返回App端console.log(event, '0000000000000000000000000')if (event.detail.data[0].action == 'uni-app') {uni.navigateBack();}}},}
</script>

2. app跳转H5使用如下方法:

//跳转到对应的webview页面并传入url和其他参数
uni.navigateTo({url: item.url.startsWith('http') ? `/pages/webview/gridWebview?title=${item.name}&src=${item.url}` : item.url})

3.H5返回app使用如下方法:

app.vue页面需要引入uni.webview.1.5.2.js文件 (目前此方法只能在真机上返回,H5上目前点击无效)

<script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script> 
onLaunch: function() {this.initScript()console.log('App Launch')},onShow: function() {console.log('App Show')},onHide: function() {console.log('App Hide')},methods: {initScript() {var script = document.createElement('script');script.type = "text/javascript";script.async = true;script.src = "./static/js/uni.webview.1.5.2.js";document.head.appendChild(script);console.log(wx, "wx");}}

在需要返回的页面添加返回方法:

goBack() {console.log('0000000', uni, )uni.webView.postMessage({data: {action: 'uni-app', //可自己随意定义,需要与app中接收通信的方法对应}});
}

 


http://www.ppmy.cn/ops/14127.html

相关文章

买卖股票+跳跃游戏 贪心算法

买卖股票的最佳时机 只收集正利润即可&#xff01; class Solution {public int maxProfit(int[] prices) {int sum0;for(int i0;i<prices.length-1;i){if(prices[i1]-prices[i]>0){//只收集正利润sumprices[i1]-prices[i];}}return sum;} } 跳跃游戏 看覆盖范围&…

共享单车(二):项目日志

stdin, stdout, stderr Linux系统下&#xff0c;当一个用户进程被创建时&#xff0c;与之对应的三个数据流&#xff08;stdin&#xff0c;stdout和stderr&#xff0c;即三个文件&#xff09;也会被创建。 stdin&#xff0c;标准输入文件&#xff0c;通常对应着终端的键盘。 s…

MySQL安装教程

目录 官网下载MSI镜像文件安装步骤选择自定义选中X64 就会出现下面红框的东西点击它 指定安装路径和数据库存放路径 基本配置配置环境变量 官网下载MSI镜像文件 安装步骤 选择自定义 选中X64 就会出现下面红框的东西 点击它 指定安装路径和数据库存放路径 不要包含中文以及空格…

Gradio 最快创建Web 界面部署到服务器并演示机器学习模型,本文提供教学案例以及部署方法,避免使用繁琐的django

最近学习hugging face里面的物体检测模型&#xff0c;发现一个方便快捷的工具&#xff01; Gradio 是通过友好的 Web 界面演示机器学习模型的最快方式&#xff0c;以便任何人都可以在任何地方使用它&#xff01; 一、核心优势&#xff1a; 使用这个开发这种演示机器学习模型的…

C# Solidworks二次开发:枚举应用实战(第三讲)

大家好&#xff0c;今天继续介绍枚举相关内容。 下面是今天要介绍的枚举&#xff1a; &#xff08;1&#xff09;第一个为swACisOutputVersion&#xff0c;这个枚举为ACIS的版本&#xff0c;下面是官方的具体解释&#xff1a; 其枚举值为&#xff1a; MemberDescriptionswAc…

表单插件——jquery.form.js

表单插件——jquery.form.js 表单插件(Form Plugin) 下载地址 :http://plugins.jquery.com/form/ 文件名:jquery.form.js version: 3.50.0-2014.02.05(最新版本) 功能:提供表单数据、重置表单项目、使用Ajax提交数据等 获取表单数据&#xff1a; 对于表单而言&#xff0c…

python爬虫之xpath4

1 最终项目实现的代码 仙剑 #!/usr/bin/env python ​ import logging import requests import re from urllib.parse import urljoin import pymongo import multiprocessing ​ mongo_client pymongo.MongoClient("mongodb://192.168.135.131:27017/") db mong…

[Swift]单元测试

编写单元测试是确保你的代码质量和功能正确性的重要步骤 一、编写单元测试的详细流程 1. 创建一个新的Xcode项目 如果你尚未创建一个项目&#xff0c;首先你需要在Xcode中创建一个新的iOS项目&#xff1a; 打开Xcode&#xff0c;选择“File” > “New” > “Project”…