关键词搜索

全站搜索
×
密码登录在这里
×
注册会员

已有账号? 请点击

忘记密码

已有账号? 请点击

使用其他方式登录

小程序跳转页面几种方式区别汇总

发布2022-05-23 浏览1993次

详情内容

在微信小程序开发时,必须用到的是小程序页面跳转,一般有大类方式,js跳转和标签跳转,下面总结一下微信小程序的所有跳转方式和区别汇总。

一:js实现

1.navigateTo (有返回键,不可以跳转到tabBar页面)

//保留当前页面,跳转到应用内的某个页面wx.navigateTo({
  url: '/pages/detail/detail?id=1'})


2.switchTab (没有返回键,只能跳转到tabBar页面,不可以携带参数)

wx.switchTab({  
      url: `/pages/detail/detail`,
    })


3.reLaunch (跳转任意页面, 没有返回, 有 首页 按钮)

    wx.reLaunch({
      url: '/pages/detail/detail'
    })


4.redirectTo ( 只可以跳转tabBar 页面, 没有返回,但有首页按钮)

    wx.redirectTo({
       url: `/pages/detail/detail`,
    })


5.navigateBack (应用在目标页面, delta值为1 ,表示跳转上一页,2表示跳两级)

   wx.navigateBack({
       delta:1
    })


区别:
1.wx.navigator是开启一个新页面,那个页面是隐藏了,原页面是onHide,所以是可以返回的,但是返回之后,跳转的页面就unload了
2.wx.redirecTo是当前页面替换成新的页面,所以返回不去onunload(页面被卸载)
3.tabBar无论跳哪个页面都是onHide
传参注意:
跳转页面传递数组参数必须序列化

 let  arr=[1,2,3,4,5]
      category = JSON.stringify(arr)        //取子集分类 数组传递需要序列化
     wx.navigateTo({
         url: `/pages/detail/detail/?cate= ${category} `,
        })


跳转页面传递数组参数必须序列化

 onLoad: function (options) {
  let   category = JSON.parse(options.cate);
 console.log(category)}


参数值过长接收时候内容不全得问题

//传参wx.navigateTo({//wx.redirectTo、wx.reLaunch
    url: '/pages/details/details?id=' + encodeURIComponent(id)
    })


//接收onLoad(options) {
    var id = decodeURIComponent(options.id);}


二:navigator组件实现

1.<navigator url = "/pages/details/details">跳转到新页面</navigator>

2.<navigator url = "/pages/details/details" open-type = "redirect">跳转到新页面</navigator>

3.<navigator url = "/pages/details/details" open-type = "switchTab">跳转到新页面</navigator>

点击QQ咨询
开通会员
上传资源赚钱
返回顶部
×
  • 微信支付
  • 支付宝付款
扫码支付
微信扫码支付
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载