路由传值
uni.navigateTo({
url:'test?id=1&name=uniapp'})
一般传一些简单数据类型的,拼接在url后面即可,跳转页面通过onLoad或者props获取对应的值,如传参过长需要用将要传的参数用JSON.stingify()转换成 字符串然后用encodeURIComponent包裹传入,
页面取值需要用decodeURIComponent()包裹取出,然后用JSON.parse()转换成复杂数据类型
encodeURIComponent(JSON.stringify(item))
JSON.parse(decodeURIComponent(option.item))
注册全局自定义事件传值
uniapp提供uni.$emit()触发全局自定义事件,第一个参数为事件名称,第二个参数为要传的参数,在需要传参的页面触发全局自定义事件
uni.$on()监听全局自定义事件,第一个参数为全局自定义事件的名称,第二个参数为回调函数,回调函数的参数为传过来的参数对象,在接收参数页面监听全局自定义事件,并在页面卸载时调用uni.$off()取消监听,参数为事件名称
uni.$emit('update',{msg:'页面更新'})
uni.$on('update',function(data){
console.log('监听到事件来自 update ,携带参数 msg 为:'+ data.msg);
})
uni.$off('update')
缓存,uniapp提供了uni.setStorage()方法存储数据到本地缓存,以及uni.getStorage()取出缓存中的数据,以及同步存储和同步取出setStorageSync(),getStorageSync
uni.setStorage({
key:'storage_key',
data:'hello',
success:function(){
console.log('success');
}})
uni.getStorage({
key:'storage_key',
success:function(res){
console.log(res.data);
}});
vuex状态管理
pinia状态管理
详见uniapp官网https://uniapp.dcloud.net.cn/api/storage/storage.html#getstorage