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
Import
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.
navigateTo
Opens a new page and pushes it onto the navigation stack.
woo.navigateTo(options: string | NavigateToOptions): Promise<void>Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
url | string | Yes | Page path, e.g. "/pages/detail/index" |
params | Record<string, string | number> | No | URL query parameters, appended automatically |
onBack | (data: unknown) => void | No | Callback when a child page returns with data |
Example:
// 简写模式
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.
navigateBack
Goes back one or more levels.
woo.navigateBack(options?: NavigateBackOptions): Promise<void>Parameters:
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
delta | number | No | 1 | How many levels to go back |
data | unknown | No | — | Data to pass to the previous page |
Example:
// 返回上一页
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).
woo.reLaunch(options: string | ReLaunchOptions): Promise<void>Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
url | string | Yes | Target page path |
params | Record<string, string | number> | No | URL query parameters |
await woo.reLaunch('/pages/index/index')navigateToTab
Switches to a TabBar page.
woo.navigateToTab(options: string | NavigateToTabOptions): Promise<void>Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
url | string | Yes | TabBar page path |
params | Record<string, string | number> | No | URL query parameters |
groupIndex | string | No | Tab group index |
await woo.navigateToTab({
url: '/pages/profile/index',
groupIndex: '2',
})reLaunchTab
Closes all pages and switches to the specified TabBar page.
woo.reLaunchTab(options: string | ReLaunchTabOptions): Promise<void>Same parameters as navigateToTab.
getCurrentPages
Returns information about the current page stack.
woo.getCurrentPages(): Promise<PageInfo[]>Return value:
interface PageInfo {
route: string // 页面路径
}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.
woo.getRoute(): RouteInfoReturn value:
interface RouteInfo {
path: string // 页面路径(不含前导 /)
query: Record<string, string> // 查询参数
}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
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
| Method | Description | Signature |
|---|---|---|
navigateTo | Open a new page and push onto the stack | (url | NavigateToOptions) → Promise<void> |
redirectTo | Close the current page and open a new one | (url | { url }) → Promise<void> |
navigateBack | Go back to the previous page | ({ delta?, data? }) → Promise<void> |
reLaunch | Close all pages and open the target | ({ url }) → Promise<void> |
switchTab | Switch to a tabBar page | ({ url }) → Promise<void> |
getCurrentPages | Get the current page stack | () → PageInfo[] |
getRouteParams | Get the current page route parameters | () → RouteParams |