0


uniapp与webview直接进行传值

uniapp与webview直接进行传值

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

H5中接收的参数:

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

webview向uniapp传值:

  1. <script>
  2. document.addEventListener('UniAppJSBridgeReady', function(){
  3. //向uniapp传值
  4. uni.postMessage({
  5. data: {
  6. action: 'message'}});
  7. uni.getEnv(function(res){
  8. console.log('当前环境:' + JSON.stringify(res));});});</script>

uniapp:

  1. <template><view class="advertisement"style="width: 100%;"><web-view :src="url" @message="message"></web-view></view></template>
标签: uni-app chrome 前端

本文转载自: https://blog.csdn.net/weixin_41823246/article/details/142213957
版权归原作者 骨子里的偏爱 所有, 如有侵权,请联系我们删除。

“uniapp与webview直接进行传值”的评论:

还没有评论