uniapp 页面跳转方法

一、保留当前页面,跳转至新页面uni.navigateTo,不能跳转tabbar页面

uni.navigateTo({ url: '/pages/login/login' })

除了跳转之外,这个方法同样支持传递参数,只需要在路径后方,通过问号的方式添加。

如:

uni.navigateTo({ url: '/pages/detail/detail?id=18&title=详情' })

id和title就是页面所传递的参数,到达目标页面后,可以在onLoad里接收:

onLoad(option) {
    console.log(option.id, option.title);
}

除此以外,如果有特殊情况想要在其他生命周期里获取,可以使用以下方法。

const routes = getCurrentPages(); // 获取所有已经打开的页面,得到是一个数组

const currentPageRoute = routes[routes.length - 1]; // 获取以打开页面的最后一项,即当前页面
console.log(currentPageRoute); // 得到一个对象,里面的options字段里,就是页面传递过来的参数
const currentPageOptions = currentPageRoute.options;
console.log(currentPageOptions.id

猜你喜欢

转载自blog.csdn.net/weixin_46167462/article/details/130624717