路由API用于实现页面跳转,常用的路由API方法有3个,分别是wx.navigateTo()、wx.redirectTo()和wx.switchTab(),具体说明如下。
他们之间的区别是:redirectTo将关闭当前页面,跳转到指定页面;navigateTo将保留当前页面,跳转到指定页面;而switchTap只能用于跳转到带tabbar的页面,并关闭其他所有非tabBar页面。
redirectTo和navigateTo在使用方式上完全相同,他们都接受一个Object对象作为参数。Object对象中最重要的属性是url,它将指定要跳转的页面路径。请注意url是页面的路径,不要加上文件的扩展名(如同app.json中定义pages一样)。
Page({ onTapJump:function(event){ wx.redirectTo({ url:"../post/post", success:function(){console.log("jump success");}, fail:function(){console.log("jump failed");}, complete:function(){console.log("jump complete");}, }); }, onUnload:function(event){console.log("page is unload");}, onHide:function(event){console.log("page is hide");}, });