zl程序教程

您现在的位置是:首页 >  其它

当前栏目

小程序开发报错:navigateTo:fail can not navigateTo a tabbar page

报错 not can 程序开发 Page fail tabBar
2023-09-27 14:22:11 时间

错误重现:

在这里插入图片描述
我新创建了一个小程序,项目暂时只有以下几个页面:

  "pages": [
    "pages/index/index",
    "pages/logs/logs",
    "pages/home/home"
  ],

同时,我在除了首页的其他页面添加了tabBar,

  "tabBar": {
    "list": [
      {
        "pagePath": "pages/home/home",
        "text": "主页"
      },
      {
        "pagePath": "pages/logs/logs",
        "text": "日志"
      }
    ]
  },

此时,我想要在index页面中添加一个点击事件,事件响应后跳转home页面,我写了如下代码:

<button type="primary" bindtap="toHomePage">进入小程序</button>
 toHomePage(){
    wx.navigateTo({
      url: '../home/home'
    });
  },

编译后点击button,出现了文章开头所示的报错。

错误分析:

经调查发现,wx.navigateTo/wx.redirectTo都不能跳转tarBar内的页面,所谓的tarBar页面,指的是app.json文件中的tarBar中注册的页面

解决报错:

解决这个报错有两种方法:
1、如果要跳转的页面确实需要在tabBar中注册,我们可以使用wx.switchTab跳转tabBar页面

  toHomePage(){
    wx.switchTab({
      url: '../home/home'
    });
  },

2、如果要跳转的页面不是必须在tabBar中注册,我们可以在tarBar项中删除目标页面对应的路径