uniapp与webview直接进行传值

uniapp与webview直接进行传值

bash 复制代码
<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中接收的参数:

bash 复制代码
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传值:

bash 复制代码
<script>
  document.addEventListener('UniAppJSBridgeReady', function() {
    //向uniapp传值
    uni.postMessage({
      data: {
        action: 'message'
      }
    });
    uni.getEnv(function(res) {
      console.log('当前环境:' + JSON.stringify(res));
    });
  });
</script>  

uniapp:

bash 复制代码
<template>
  <view class="advertisement" style="width: 100%;">
    <web-view :src="url" @message="message"></web-view>
  </view>
</template>
相关推荐
zhuà!23 分钟前
腾讯地图TMap标记反显,新增标记
前端·javascript·vue.js
未知原色26 分钟前
web worker使用总结(包含多个worker)
前端·javascript·react.js·架构·node.js
ttod_qzstudio37 分钟前
CSS改变图片颜色方法介绍
前端·css
curdcv_po1 小时前
我接入了微信小说小程序官方阅读器
前端·微信小程序
程序员鱼皮1 小时前
什么是 RESTful API?凭什么能流行 20 多年?
前端·后端·程序员
www_stdio1 小时前
让大语言模型拥有“记忆”:多轮对话与 LangChain 实践指南
前端·langchain·llm
inferno1 小时前
JavaScript 基础
开发语言·前端·javascript
cindershade1 小时前
Intersection Observer 的实战方案
前端
青莲8431 小时前
Kotlin Flow 深度探索与实践指南——中部:实战与应用篇
android·前端
cindershade1 小时前
事件委托(Event Delegation)的原理
前端