Skip to content

Files

Latest commit

author
staven
Apr 23, 2019
79ba9d0 · Apr 23, 2019

History

History
44 lines (42 loc) · 1.16 KB

小程序获取当前页面信息.md

File metadata and controls

44 lines (42 loc) · 1.16 KB
// 获取加载的页面
const pages = getCurrentPages() 
// 获取当前页面的对象
const currentPage = pages[pages.length-1] 
// 当前页面url
const url = currentPage.route 
// 如果要获取url中所带的参数可以查看options
const options = currentPage.options 

工具函数

/*  获取当前页 */
export const getCurrentPage = () => {
    //  获取加载的页面
    const pages = getCurrentPages()
    // 获取当前页面的对象
    return pages[pages.length-1] 
}
/*获取当前页url*/
export const getCurrentPageUrl = () => {
     // 当前页面url
    return getCurrentPage().route
}

/*获取当前页带参数的url*/
export const getCurrentPageParams = () => {
    // 获取当前页面的对象
    const currentPage = getCurrentPage()
    // 当前页面url
    const url = currentPage.route 
     // 如果要获取url中所带的参数可以查看options
    const options = currentPage.options
    
    //拼接url的参数
    let params= url + '?'
    for(var key in options) {
        let value = options[key]
        params += key + '=' + value + '&'
    }
    params =params .substring(0, params.length-1)
    return params
}