一、web-view 从uniapp应用向html传参
在HTML中动态添加web-view的src:
<web-view :src="`/static/html/index.html?userInfo=${params}`"></web-view>
获取本地存储的用户信息 userInfo
在这里,由于传入的用户信息较多,需要传入json数据,但是由于在地址栏的json直接使用JSON.parse()解析,会报错;所以需要对传递的参数使用 encodeURIComponent()把字符串作为 URI 组件进行编码。
export default {
name: 'mapShow',
data() {
return {
params:''
}
},
onShow() {
this.gotoview();
},
methods: {
gotoview(){
let _this = this;
uni.getStorage({
key: 'userInfo',
success: function (res) {
let userInfo = JSON.parse(res.data);
let data = {
authentication_token:userInfo.authentication_token, //token
navugation_bar:[...userInfo.navugation_bar] //其他参数
}
let data1 = encodeURIComponent(JSON.stringify(data)) //编码
_this.params = data1;
}
});
}
}
}
获取用户信息存入本地
在接收参数页面,获取地址的参数,将参数用decodeURIComponent解析后,再用使用JSON.parse。
var str=window.location.search;
var nt=window.location.search.indexOf('=');
var userInfo = decodeURIComponent(str.substring(nt + 1)) //解码
userInfo = JSON.stringify(JSON.parse(userInfo))
localStorage.setItem('userInfo', userInfo);
sessionStorage.setItem('userInfo', userInfo);
从此,就可以把用户信息从uniapp应用完美的传入HTML页面了。
二、左滑返回上一页
onBackPress() {
//#ifdef APP-PLUS
uni.navigateTo({
url: '/pages/personalCenter/index'
})
//#endif
},