Skip to content

Router

The Router API manages the mini program’s page navigation stack. All page paths are based on the pages array defined in app.json.

Try it online

navigateTonavigateBackredirectToreLaunch

Router API demo

Click a button to trigger a routing call. The console shows SDK logs.

Import

ts
import woo from 'mini-sdk'
// 或
import { router } from 'mini-sdk'

API reference

Quick lookup

At the bottom of the page, see the full API quick reference to find method signatures quickly.

Opens a new page and pushes it onto the navigation stack.

ts
woo.navigateTo(options: string | NavigateToOptions): Promise<void>

Parameters:

FieldTypeRequiredDescription
urlstringYesPage path, e.g. "/pages/detail/index"
paramsRecord<string, string | number>NoURL query parameters, appended automatically
onBack(data: unknown) => voidNoCallback when a child page returns with data

Example:

ts
// 简写模式
await woo.navigateTo('/pages/detail/index')

// 携带参数
await woo.navigateTo({
  url: '/pages/detail/index',
  params: { id: 123, type: 'article' },
})
// 实际跳转:/pages/detail/index?id=123&type=article

// 接收子页面返回数据
await woo.navigateTo({
  url: '/pages/edit/index',
  onBack(data) {
    console.log('子页面返回数据:', data)
  },
})

onBack behavior

onBack is managed with a LIFO stack, so multiple navigateTo calls to the same page do not interfere. When reLaunch / navigateToTab reset the route stack, all pending onBack callbacks are cleared automatically.


Goes back one or more levels.

ts
woo.navigateBack(options?: NavigateBackOptions): Promise<void>

Parameters:

FieldTypeRequiredDefaultDescription
deltanumberNo1How many levels to go back
dataunknownNoData to pass to the previous page

Example:

ts
// 返回上一页
await woo.navigateBack()

// 返回两级并携带数据
await woo.navigateBack({
  delta: 2,
  data: { result: 'success', value: 42 },
})

reLaunch

Closes all pages and opens the target page (resets the navigation stack).

ts
woo.reLaunch(options: string | ReLaunchOptions): Promise<void>

Parameters:

FieldTypeRequiredDescription
urlstringYesTarget page path
paramsRecord<string, string | number>NoURL query parameters
ts
await woo.reLaunch('/pages/index/index')

Switches to a TabBar page.

ts
woo.navigateToTab(options: string | NavigateToTabOptions): Promise<void>

Parameters:

FieldTypeRequiredDescription
urlstringYesTabBar page path
paramsRecord<string, string | number>NoURL query parameters
groupIndexstringNoTab group index
ts
await woo.navigateToTab({
  url: '/pages/profile/index',
  groupIndex: '2',
})

reLaunchTab

Closes all pages and switches to the specified TabBar page.

ts
woo.reLaunchTab(options: string | ReLaunchTabOptions): Promise<void>

Same parameters as navigateToTab.


getCurrentPages

Returns information about the current page stack.

ts
woo.getCurrentPages(): Promise<PageInfo[]>

Return value:

ts
interface PageInfo {
  route: string  // 页面路径
}
ts
const pages = await woo.getCurrentPages()
console.log('当前页面栈:', pages)
// [{ route: 'pages/index/index' }, { route: 'pages/detail/index' }]

getRoute

Synchronously returns the current page path and query parameters.

ts
woo.getRoute(): RouteInfo

Return value:

ts
interface RouteInfo {
  path: string                    // 页面路径(不含前导 /)
  query: Record<string, string>   // 查询参数
}
ts
const { path, query } = woo.getRoute()
console.log(path)   // "pages/detail/index"
console.log(query)  // { id: "123" }

Routing modes

  • Simulator: Hash routing (http://localhost/#/pages/detail/index?id=1)
  • Native: Pathname routing (https://host/pages/detail/index?id=1)

getRoute() works with both.

Type definitions

ts
interface NavigateToOptions {
  url: string
  params?: Record<string, string | number | undefined>
  onBack?: (data: unknown) => void
}

interface NavigateBackOptions {
  delta?: number
  data?: unknown
}

interface ReLaunchOptions {
  url: string
  params?: Record<string, string | number | undefined>
}

interface NavigateToTabOptions {
  url: string
  params?: Record<string, string | number | undefined>
  groupIndex?: string
}

interface ReLaunchTabOptions {
  url: string
  params?: Record<string, string | number | undefined>
  groupIndex?: string
}

interface PageInfo {
  route: string
}

interface RouteInfo {
  path: string
  query: Record<string, string>
}

type RouteParams = Record<string, string | number | undefined>

API quick reference

MethodDescriptionSignature
navigateToOpen a new page and push onto the stack(url | NavigateToOptions) → Promise<void>
redirectToClose the current page and open a new one(url | { url }) → Promise<void>
navigateBackGo back to the previous page({ delta?, data? }) → Promise<void>
reLaunchClose all pages and open the target({ url }) → Promise<void>
switchTabSwitch to a tabBar page({ url }) → Promise<void>
getCurrentPagesGet the current page stack() → PageInfo[]
getRouteParamsGet the current page route parameters() → RouteParams

MiniDev Studio — Mini-app Development Toolkit