uniapp 获取当前页面的路径以及参数

2,515 阅读1分钟
// 获取页面路径以及参数
export const getPagePath = function () {
	const pages = getCurrentPages();
	const currentPage = pages[pages.length - 1];
	const currentPath = currentPage.__route__ || currentPage.route;
	const query = currentPage.options;
	let path = '/' + currentPath;
	Object.entries(query).forEach(([key, value], index) => {
		if (index == 0) {
			path += '?';
		}
		path += `${key}=${value}${index < Object.keys(query).length - 1 ? '&' : ''}`;
	});
	console.log('页面路径以及参数:', path);
	return path;
};