1. 获取当前页url
/**
* 获取当前页url
*/
const $getCurrentPageUrl = () => {
let pages = getCurrentPages() // 获取加载的页面
let currentPage = pages[pages.length - 1] // 获取当前页面的对象
let url = currentPage.route // 当前页面url
return url
}
2. 获取当前页带参数的url
/**
* 获取当前页带参数的url
*/
const $getCurrentPageUrlWithArgs = () => {
let pages = getCurrentPages() // 获取加载的页面
let currentPage = pages[pages.length - 1] // 获取当前页面的对象
let url = currentPage.route // 当前页面url
let options = currentPage.options // 如果要获取url中所带的参数可以查看options
if (Object.keys(options).length > 0) {
// 拼接url的参数
let urlWithArgs = url + '?'
for (let key in options) {
let value = options[key]
urlWithArgs += key + '=' + value + '&'
}
urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
return urlWithArgs
} else {
// 拼接url的参数
let urlWithArgs = url
return urlWithArgs
}
}
/**
* 获取当前页带参数的url Promise 封装
*/
const $getCurrentPageUrlWithArgsPromise = () => {
return new Promise((resolve, reject) => {
let urlWithArgs = $getCurrentPageUrlWithArgs()
if (urlWithArgs) {
resolve(urlWithArgs)
} else {
reject('error')
}
})
}