uni-app跳转(传参)+设置获取缓存

一、uni-app页面跳转,跳转传参,参数过长处理

官方API: uni-app官网

1、uni.navigateTo 保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面。

注意:页面跳转再back回来的时候,触发单页onShow,页面不重载,可保持原页状态。

例:uni.navigateTo({ url: '/pages/adult/inConsent' }) 
    
    对应url是在pages.json中配置的路由。

2、uni.redirectTo 关闭当前页面,跳转到应用内的某个页面。

 注意:用法同上;重定向后原页将关闭,再去进去触发单页onLoad,页面重载。

3、uni.switchTab 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。

注意:由tabBar切换跳转的页面,要回该页必须使用uni.switchTab,使用uni.navigateBack无效。

例:uni.switchTab({ url: '/pages/adult/adultInfo' })
    
    对应url是在pages.json中配置的路由。

4、跳转传参:短参使用类似get的传参方法;长参使用encodeURIComponent。

短参:
// 跳转传参
uni.navigateTo({ url: '/pages/practice/randomPractice?id=1&name=uniapp' })

// 页面onLoad接受参数
export default {
    onLoad: function (option) { //option为object类型,会序列化上个页面传递的参数
        console.log(option.id); //打印出上个页面传递的参数。
        console.log(option.name); //打印出上个页面传递的参数。
    }
}

长参:
// 浏览视频
const video = {
    nam: item.nam,
    url: url,
    img: item.cover != null ? (this.$global.FileUrl + item.cover) : '',
    id: item.id
}
// 跳转传参
uni.navigateTo({
    url: '/pages/search/playVideo?video=' + encodeURIComponent(JSON.stringify(video))
})

// 页面onLoad接受参数
onLoad(option) {
    // 获取视频信息
    const video = JSON.parse(decodeURIComponent(option.video))
    this.title = video.nam
    this.url = video.url
    this.img = video.img
    // 设置小程序页面标题 为视频标题
    uni.setNavigationBarTitle({ title: this.title })
}

二、uni-app设置获取缓存

官方API:uni-app官网

1、首先移动端与浏览器不同,不存在cookie/session,移动端只有实体缓存localstorage

2、同步设置获取缓存(实体)的API:
        设置 uni.setStorageSync(KEY,DATA)
        获取 uni.getStorageSync(KEY)

uni.setStorageSync('storage_key', 'hello');
uni.setStorageSync('zone', { updateTime: new Date(), zoneTree: zoneTree })

uni.getStorageSync('storage_key') // 获取的是字符串
uni.getStorageSync('zone') // 获取的是map对象

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的登录页面的uni-app实现: ```vue <template> <view class="container"> <view class="form"> <view class="input-wrapper"> <input type="text" placeholder="请输入账号" v-model="username" /> </view> <view class="input-wrapper"> <input type="password" placeholder="请输入密码" v-model="password" /> </view> <view class="button-wrapper"> <button type="primary" @click="login">登录</button> </view> </view> </view> </template> <script> export default { data() { return { username: '', password: '' } }, methods: { login() { // 发送登录请求 uni.request({ url: 'http://example.com/login', method: 'POST', data: { username: this.username, password: this.password }, success: (res) => { // 登录成功,将用户信息存储到本地缓存 uni.setStorageSync('userInfo', res.data) // 到首页 uni.navigateTo({ url: '/pages/index/index' }) }, fail: (err) => { // 登录失败,提示用户 uni.showToast({ title: '登录失败,请检查账号密码是否正确', icon: 'none' }) } }) } } } </script> <style> .container { display: flex; justify-content: center; align-items: center; height: 100vh; } .form { width: 80%; } .input-wrapper { margin-bottom: 20px; } button { width: 100%; height: 40px; line-height: 40px; text-align: center; background-color: #007aff; color: #fff; border-radius: 5px; } </style> ``` 这个登录页面包含一个账号输入框、一个密码输入框和一个登录按钮。当用户点击登录按钮时,会发送一个POST请求到服务器进行登录验证。如果登录成功,会将用户信息存储到本地缓存,并到首页。如果登录失败,会提示用户登录失败。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值