uni-app getCurrentPages() 的options参数问题
uni-app getCurrentPages() 的options参数问题
示例代码:
let routes = getCurrentPages();
let currentRoute = routes[routes.length - 1];
var data = {};
// #ifdef H5
currentRoute = currentRoute.$page
// #endif
data = {
route: currentRoute.route,
options: currentRoute.options
}
return data;
操作步骤:
- 复制 代码示例
- 会发现 H5下 不直接返回 options参数 要在
$page
里获取,但是到了小程序里面 并不返回$page
里的options
参数 要直接获取
预期结果:
- 统一一下
实际结果:
- 1
bug描述:
在 vue2 中
let routes = getCurrentPages();
let currentRoute = routes[routes.length - 1];
route: currentRoute.route,
options: currentRoute.options,
各端都能获取到路由和参数信息,方便储存和做下一步处理。
但是更新到 vue3 以后
需要和代码示例一样才能获取到。H5中 已经不再返回 options
参数,但是到了小程序端又不返回 $page
参数,那这样的话就非常不方便,而且感觉很慌,数据返回的就不统一。
HBuilderX 4.0.2024012711-alpha 已修复。
收到,后续会考虑统一暴露 options 属性到 page 对象。
不要后续,就现在,我弄完我晚上还要和小姐姐吃饭呢。我等你。
你都使用 #ifdef H5了
里面使用window.location一样的
哈哈,我只是演示出来有这么个意思。本来并不需要
let page = getCurrentPages()[0];
–小程序写法跟vue2一致,没有问题
let data = page.data;
let options = page.options;
–app端需要这样写才行
let data = page.$vm.$data;
let options = page.$page.options;
我用的是4.29版本。
太痛苦了,没有文档可查。只能猜测尝试。
在 uni-app
中,getCurrentPages()
是一个用于获取当前页面栈的方法。它返回一个数组,数组中的每个元素代表一个页面实例。每个页面实例都包含一些属性和方法,其中 options
是一个常见的属性,用于获取页面跳转时传递的参数。
options
参数的作用
options
是一个对象,包含了页面跳转时通过 URL 传递的参数。例如,当你使用 uni.navigateTo
或 uni.redirectTo
等方法跳转页面时,可以通过 URL 的 query 参数传递数据,这些数据会被解析到 options
对象中。
示例
假设你有一个页面跳转的代码如下:
uni.navigateTo({
url: '/pages/detail/detail?id=123&name=test'
});
在目标页面(/pages/detail/detail
)中,你可以通过 getCurrentPages()
获取当前页面栈,并访问 options
参数:
onLoad() {
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const options = currentPage.options;
console.log(options.id); // 输出: 123
console.log(options.name); // 输出: test
}