WeChat Open Platform

Mini Programs Documentation

wx.navigateTo(OBJECT)

Retains current page and redirects to a page within the application. You can return to the original page by using wx.navigateBack.

Object parameter descriptions:

Parameter Type Required Description
url String Yes The path required to redirect to a page that is not on the tabBar within the application. The path can have parameters after it. ? is used to separate the parameters and the path. = is used to connect parameter keys and parameter values. & is used to separate different parameters. For example: 'path?key=value&key2=value2'
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code:

wx.navigateTo({
  url: 'test?id=1'
})
//test.js
Page({
  onLoad: function(option){
    console.log(option.query)
  }
})

Note: We specify that page paths can only have five levels to prevent complications for users when they are using a Mini Program. Please try to avoid multilevel interactive modes.

wx.redirectTo(OBJECT)

Closes current page and redirects to a page within the application.

Object parameter descriptions:

Parameter Type Required Description
url String Yes The path required to redirect to a page that is not on the tabBar within the application. The path can have parameters after it. ? is used to separate the parameters and the path. = is used to connect parameter keys and parameter values. & is used to separate different parameters. For example: 'path?key=value&key2=value2'
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code:

wx.redirectTo({
  url: 'test?id=1'
})

wx.reLaunch(OBJECT)

Closes all pages and opens a page within the application.

Object parameter descriptions:

Parameter Type Required Description
url String Yes The path required to redirect to a page that is not on the tabBar within the application. The path can have parameters after it. ? is used to separate the parameters and the path. = is used to connect parameter keys and parameter values. & is used to separate different parameters. For example: 'path?key=value&key2=value2'
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code:

wx.reLaunch({
  url: 'test?id=1'
})
//test.js
Page({
  onLoad: function(option){
    console.log(option.query)
  }
})

wx.switchTab(OBJECT)

Redirects to the tabBar page and closes all other non-tabBar pages.

Object parameter descriptions:

Parameter Type Required Description
url String Yes Path required to redirect to the tabBar page (page needs to be defined in the app.json tabBar field), the path cannot have parameters after it
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code:

{
  "tabBar": {
    "list": [{
      "pagePath": "index",
      "text": "Homepage"
    },{
      "pagePath": "other",
      "text": "Other"
    }]
  }
}
wx.switchTab({
  url: '/index'
})

wx.navigateBack(OBJECT)

Closes the current page and returns to the previous page or pages. The current page stack can be obtained using getCurrentPages(). This can be used to determine how many levels to go back.

Object parameter descriptions:

Parameter Type Default value Description
delta Number 1 The number of pages navigated backwards, you will be returned to the homepage if delta is greater than the existing number of pages

Sample code:

// Note: When calling navigateTo to redirect, the page calling this method will be added to the stack. This will not happen if the redirectTo method is called. See sample code below

// This is page A
wx.navigateTo({
  url: 'B?id=1'
})

// This is page B
wx.navigateTo({
  url: 'C?id=1'
})

// navigateBack in page C will return you to page A
wx.navigateBack({
  delta: 2
})

Tip

  1. tip: wx.navigateTo and wx.redirectTo are not permitted to redirect to the tab bar page. You can only use wx.switchTab to redirect to the tab bar page.