小程序 详细

发布时间 2023-12-06 10:53:41作者: JaneLifeVlog

小程序界面间的跳转

**保留当前页面,只能打开非 tabBar 页面,返回时返回该页面**
wx.navigateTo({
  url: '路径地址',
})

**关闭卸载当前页面,只能打开非 tabBar 页面,**
wx.redirectTo({
  url: '路径地址'
})

**关闭所有非tabbar页面, 只能打开 tabBar 页面**
wx.switchTab({
  url: '路径地址'
})

**关闭卸载所有页面,可以打开任意页面**
wx.reLaunch({
  url: '路径地址'
})

**返回前面的页面,可以指定返回多少页,如果用过redirectTo,那么被关闭的页面将返回不去**
wx.navigateBack({
  delta: 2  //返回的页面数,如果 delta 大于现有页面数,则返回到首页。
})

小程序页面间的传参

wx.switchTab({
  url: '../todolist/todolist?id=789',
})

//或者navigator标签
<navigator url="../detail/detail?id=666">带参数去detail</navigator>  

跳转到指定界面之后,可以在该页面的onLoad方法中的options参数(本身是个对象)拿到路由跳转的参数。
onLoad(options) {
  console.log(options);
},

本地存储

(一)同步
1.存储:wx.setStorageSync('list', {age:5})
2.获取:wx.getStorageSync('list')
//本地同步缓存
syncSet(){
  console.log('这是同步缓存');
  wx.setStorageSync('sync', {content:'这是同步缓存'})
},
//本地同步获取
syncGet(){
  console.log(wx.getStorageSync('sync'));
},

(二)异步
1.存储:wx.setStorage({ })
2.获取:wx.getStorage({ })
//本地异步存储
asyncSet(){
  wx.setStorage({
    key:'async',
    data:'这是异步存储的数据',
    success(){
      console.log('异步存储');
     }
   })
},
//本地异步获取
asyncGet(){
  wx.getStorage({
    key:'async',
    success(res){
      console.log(res);
     }
   })
},