当前位置: 代码网 > it编程>编程语言>Javascript > uniapp与webview直接进行传值的代码示例

uniapp与webview直接进行传值的代码示例

2024年10月28日 Javascript 我要评论
uniapp与webview直接进行传值<template> <view class="advertisement" style="width: 100%;"> <

uniapp与webview直接进行传值

<template>
  <view class="advertisement" style="width: 100%;">
    <web-view :src="url" @message="message"></web-view>
  </view>
</template>
  
<script>
export default {
  data() {
    return {
      url:'/hybrid/html/local.html?data='
    };
  },
  onload(data) {<br>          //这里对要传入到webview中的参数进行encodeuricomponent编码否则中文乱码
    this.url+=encodeuricomponent(data.data)
  },
  mounted() {},
  methods: {
    message(event){
      console.log(event.detail.data);
    }
  }
};
</script>
  
<style scoped="scoped" lang="scss">
@import './advertisement.scss';
</style>

h5中接收的参数:

console.log(getquery('data')); //获取 uni-app 传来的值
             
      //取url中的参数值
      function getquery(name) {
        // 正则:[找寻'&' + 'url参数名字' = '值' + '&']('&'可以不存在)
        let reg = new regexp("(^|&)"+ name +"=([^&]*)(&|$)");
        let r = window.location.search.substr(1).match(reg);
        console.log(r);
        if(r != null) {
          // 对参数值进行解码
          return decodeuricomponent(r[2]);
        }
        return null;
      }

webview向uniapp传值:

<script>
  document.addeventlistener('uniappjsbridgeready', function() {
    //向uniapp传值
    uni.postmessage({
      data: {
        action: 'message'
      }
    });
    uni.getenv(function(res) {
      console.log('当前环境:' + json.stringify(res));
    });
  });
</script>  

uniapp:

<template>
  <view class="advertisement" style="width: 100%;">
    <web-view :src="url" @message="message"></web-view>
  </view>
</template>

总结 

到此这篇关于uniapp与webview直接进行传值的文章就介绍到这了,更多相关uniapp与webview直接传值内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com