uniapp與webview之間的相互傳值的實(shí)現(xiàn)
1.uni-app 如何發(fā)送數(shù)據(jù)到 H5? 其實(shí)很接單、在 web-view 中只需要通過 URL 就可以向 H5 進(jìn)行傳參 例如在 uni-app 中:
<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> //這里對(duì)要傳入到webview中的參數(shù)進(jìn)行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中的參數(shù)值
function getQuery(name) {
// 正則:[找尋'&' + 'url參數(shù)名字' = '值' + '&']('&'可以不存在)
let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
let r = window.location.search.substr(1).match(reg);
console.log(r);
if(r != null) {
// 對(duì)參數(shù)值進(jìn)行解碼
return decodeURIComponent(r[2]);
}
return null;
}
2.webview向uniapp傳值
<script>
document.addEventListener('UniAppJSBridgeReady', function() {
//向uniapp傳值
uni.postMessage({
data: {
action: 'message'
}
});
uni.getEnv(function(res) {
console.log('當(dāng)前環(huán)境:' + JSON.stringify(res));
});
});
</script>
uniapp接受
//message接受方法
<template>
<view class="advertisement" style="width: 100%;">
<web-view :src="url" @message="message"></web-view>
</view>
</template>
到此這篇關(guān)于uniapp與webview之間的相互傳值的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)uniapp與webview相互傳值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用javascript取得傳遞參數(shù)的個(gè)數(shù)的代碼
用javascript取得傳遞參數(shù)的個(gè)數(shù)的代碼...2007-10-10
phantomjs導(dǎo)出html到pdf的方法總結(jié)
這篇文章主要介紹了phantomjs導(dǎo)出html到pdf的方法總結(jié),需要的朋友可以參考下2017-10-10
js實(shí)現(xiàn)省市級(jí)聯(lián)效果分享
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)省市級(jí)聯(lián)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
小程序測(cè)試后臺(tái)服務(wù)的方法(ngrok)
這篇文章主要介紹了小程序測(cè)試后臺(tái)服務(wù)的方法(ngrok),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03

