wx.switchTab失效

wx.switchTab失效


我在app.js的onLaunch当中wx.getStorage获取cookie字段,如果存在则直接通过wx.switchTab跳转到主页。但是失效,并不会跳转,不过switchTabsuccess回调会执行。

app.js onLaunch

onLaunch: function() {
    let that = this;
    // 展示本地存储能力
    wx.getStorage({
      key: 'cookie',
      success: function(res) {
        that.globalData.cookie = res.data
        wx.switchTab({
          url: '/pages/home/index',
          success:res=>{
            console.log(res);
          },
          fail:err=>{
            console.log(err)
          }
        })
      }), 

app.json

{
  "pages": [
    "pages/login/login",
    "pages/home/index",
    "pages/me/index",
    "pages/news/index",
    "pages/home/pages/leave/index",
    "pages/home/pages/count/index",
    "pages/home/pages/count/pages/next/next",
    "pages/home/pages/profile/index"
  ],
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "WeChat",
    "navigationBarTextStyle": "black"
  },
  "tabBar": {
    "color": "#a9b7b7",
    "selectedColor": "#11cd6e",
    "borderStyle": "white",
    "list": [
      {
        "selectedIconPath": "images/index/index.png",
        "iconPath": "images/index/index.png",
        "pagePath": "pages/home/index",
        "text": "首页"
      },
      {
        "selectedIconPath": "images/index/message.png",
        "iconPath": "images/index/message.png",
        "pagePath": "pages/news/index",
        "text": "消息中心"
      },
      {
        "selectedIconPath": "images/index/user.png",
        "iconPath": "images/index/user.png",
        "pagePath": "pages/me/index",
        "text": "我"
      }
    ]
  }
}
阅读 4.8k
2 个回答

switchTab 跳转的 url 是相对当前 page 的相对地址,我理解可能是 ../home/index

app.js的onLaunch和onShow在执行后才会注册页面,所以这时无法进行跳转,可以放到启动页面上,或者在app.js中setTimeout

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题