微信小程序页面跳转
时间: 2023-07-13 07:21:46 浏览: 112
微信小程序页面跳转可以通过以下两种方式实现:
1. 使用 `<navigator>` 组件进行跳转
在小程序中,可以使用 `<navigator>` 组件来实现页面跳转。该组件有以下属性:
- `url`: 跳转的链接地址,可以是相对路径或绝对路径。
- `open-type`: 跳转方式,可选值为 `navigate`(保留当前页面,跳转到应用内的某个页面)、`redirect`(关闭当前页面,跳转到应用内的某个页面)、`switchTab`(跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面)等。
例如:
```html
<navigator url="/pages/detail/detail?id=1" open-type="navigate">跳转到详情页</navigator>
```
2. 使用 `wx.navigateTo`、`wx.redirectTo`、`wx.switchTab` 等 API 进行跳转
除了使用 `<navigator>` 组件进行跳转外,还可以通过调用小程序提供的 API 来实现页面跳转。常用的 API 包括:
- `wx.navigateTo`:保留当前页面,跳转到应用内的某个页面。
- `wx.redirectTo`:关闭当前页面,跳转到应用内的某个页面。
- `wx.switchTab`:跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
例如:
```javascript
wx.navigateTo({
url: '/pages/detail/detail?id=1'
});
```