Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

面试官:说说微信小程序中路由跳转的方式有哪些?区别? #15

Open
linwu-hi opened this issue Jul 30, 2023 · 0 comments

Comments

@linwu-hi
Copy link
Owner

linwu-hi commented Jul 30, 2023

面试官:说说微信小程序中路由跳转的方式有哪些?区别?

一、wx.navigateTo

保留当前页面,跳转到应用内的某个页面。可以使用wx.navigateBack回到原页面。

特点:页面栈增多,占用内存。

参数:

{
  url: 'test?id=1' // 需要跳转的应用内非 tabBar 的页面的路径
} 

二、wx.redirectTo

关闭当前页面,跳转到应用内的某个页面。

特点:不能回到原页面,页面栈不增加。

参数:

{
  url: 'test?id=1' // 需要跳转的应用内非 tabBar 的页面的路径
}

三、wx.switchTab

跳转到 tabBar 页面,关闭其他所有非 tabBar 页面。

特点:页面栈清空只留下新的 tabBar 页面。

参数:

{
  url: 'test' // 需要跳转的 tabBar 页面的路径
}

四、wx.navigateBack

关闭当前页面,返回上一页面或多级页面。

特点:页面栈逐级退出,减少内存占用。

参数:

{
  delta: 1 // 返回的页面数
}

五、wx.reLaunch

关闭所有页面,打开到应用内的某个页面。

特点:页面栈清空只留下新的页面。

参数:

{
  url: 'test' // 需要跳转的页面路径 
}

总结

根据需要选择合适的页面跳转方式,控制页面栈数量,合理管理小程序内存占用。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant