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


    1. 在app项目添加webview文件夹添加gridWebview.vue文件。代码如下
    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. src: '',
    6. }
    7. },
    8. computed: {
    9. token() {
    10. return this.$store.state.login.token || ''
    11. },
    12. roles() {
    13. return this.$store.state.login.roles || ''
    14. },
    15. userInfo(){
    16. return this.$store.state.login.userInfo || ''
    17. }
    18. },
    19. onLoad(opt) {
    20. console.log(opt)
    21. //拼接所需参数
    22. this.title = opt.title
    23. this.src = opt.src
    24. + (opt.src.includes('?') ? '&' : '?')
    25. + (this.token.length>0 ? 'token=' + this.token : '')
    26. + (this.userInfo.phone.length>0 ?`&phone=${this.userInfo.phone}` :'' )
    27. + (this.userInfo.userId.length>0 ?`&userId=${this.userInfo.userId}` :'' )
    28. + (this.userInfo.username.length>0 ?`&username=${this.userInfo.username}` :'' )
    29. + (opt.data?`&data=${opt.data}` :'' )
    30. + (opt.cityid?`&cityid=${opt.cityid}` :'' )
    31. },
    32. onShow() {
    33. // #ifdef APP-PLUS
    34. 设置webview显示时顶部丢失问题
    35. var height = 0; //定义动态的高度变量,如高度为定值,可以直接写
    36. uni.getSystemInfo({
    37. //成功获取的回调函数,返回值为系统信息
    38. success: (sysinfo) => {
    39. height = sysinfo.windowHeight -
    40. 40; //自行修改,自己需要的高度 此处如底部有其他内容,可以直接---(-50)这种,如果不减,页面会下沉,看不到底部导航栏
    41. },
    42. complete: () => {}
    43. });
    44. var currentWebview = this.$scope.$getAppWebview(); //获取当前web-view
    45. setTimeout(function() {
    46. var wv = currentWebview.children()[0];
    47. wv.setStyle({ //设置web-view距离顶部的距离以及自己的高度,单位为px
    48. top: 40, //此处是距离顶部的高度,应该是你页面的头部
    49. height: height, //webview的高度
    50. scalable: false, //webview的页面是否可以缩放,双指放大缩小,
    51. })
    52. }, 500); //如页面初始化调用需要写延迟
    53. // #endif
    54. },
    55. methods: {
    56. customBack() {
    57. // 在Webview页面中调用uni.navigateBack()
    58. uni.navigateBack();
    59. },
    60. getMessage(event) { //在H5端使用通信返回App端
    61. console.log(event, '0000000000000000000000000')
    62. if (event.detail.data[0].action == 'uni-app') {
    63. uni.navigateBack();
    64. }
    65. }
    66. },
    67. }
    68. script>

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

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

    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> 
    1. onLaunch: function() {
    2. this.initScript()
    3. console.log('App Launch')
    4. },
    5. onShow: function() {
    6. console.log('App Show')
    7. },
    8. onHide: function() {
    9. console.log('App Hide')
    10. },
    11. methods: {
    12. initScript() {
    13. var script = document.createElement('script');
    14. script.type = "text/javascript";
    15. script.async = true;
    16. script.src = "./static/js/uni.webview.1.5.2.js";
    17. document.head.appendChild(script);
    18. console.log(wx, "wx");
    19. }
    20. }

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

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

     

  • 相关阅读:
    聊一聊对一个 C# 商业程序的反反调试
    【PCBA方案】充电宝打气泵方案充气模块设计
    【第三趴】uni-app页面搭建与配置(了解工程目录结构、学会搭建页面、配置页面并成功运行)
    uniapp新版微信小程序用户隐私协议授权
    python————函数与模块化编程,含日历展示的实现
    【Java】String类的理解及字符串常量池
    javascript事件处理二 事件对象event详解及target和currentTarget区别
    Git小乌龟(TortoiseGit) 简单提交代码到github
    若依框架代码生成详解
    【linux】编译器使用
  • 原文地址:https://blog.csdn.net/qq_42103673/article/details/138148446