在小程序中,可以使用 wx.redirectTo()
或 wx.reLaunch()
来实现类似于 React Router 中的 history.replace()
功能。以下是具体的方法:
- 使用
wx.redirectTo()
: 如果你希望跳转到一个新页面,并关闭当前页面,但保留页面栈中的其他页面,可以使用redirectTo
。请注意,redirectTo
只能用于打开非 tabBar 页面。
wx.redirectTo({
url: `/pages/home/home?hotelId=${hotelId || ''}`
});
- 使用
wx.reLaunch()
: 如果你希望关闭所有页面并跳转到一个新的页面,可以使用reLaunch
。这会清空整个历史记录栈,相当于完全重启小程序。
wx.reLaunch({
url: `/pages/home/home?hotelId=${hotelId || ''}`
});
根据你的具体需求选择合适的方法。如果你只需要替换当前页面,则推荐使用 wx.redirectTo()
;如果需要完全重新加载并清空历史记录,则使用 wx.reLaunch()
。
内容由零声教学AI助手提供,问题来源于学员提问