微信小程序獲取當(dāng)前頁(yè)面路徑及參數(shù)封裝
小程序獲取當(dāng)前頁(yè)面
在小程序中,所有頁(yè)面的路由都由框架統(tǒng)一管理。
框架以棧的形式維護(hù)了當(dāng)前的所有頁(yè)面。
getCurrentPages() 函數(shù)用于獲取當(dāng)前頁(yè)面棧的實(shí)例,以數(shù)組形式按棧的順序給出,第一個(gè)元素為首頁(yè),最后一個(gè)元素為當(dāng)前頁(yè)面。
注意:
1.不要嘗試修改頁(yè)面棧,會(huì)導(dǎo)致路由以及頁(yè)面狀態(tài)錯(cuò)誤。
2.不要在 App.onLaunch 的時(shí)候調(diào)用 getCurrentPages(),此時(shí) page 還沒(méi)有生成。
export function getCurrentPageUrl() { const pages = getCurrentPages() const currentPage = pages[pages.length - 1] const url = `/${currentPage.route}` return url }
小程序獲取當(dāng)前頁(yè)面路徑及參數(shù)
export function getCurrentPageUrlWithArgs() { const pages = getCurrentPages() const currentPage = pages[pages.length - 1] const url = currentPage.route const options = currentPage.options let urlWithArgs = `/${url}?` for (let key in options) { const value = options[key] urlWithArgs += `${key}=${value}&` } urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1) return urlWithArgs }
由此可推,獲取上一個(gè)頁(yè)面 則是pages.lenght-2, 封到工具類(lèi)里util.js非常實(shí)用
如果您的問(wèn)題還未解決可以聯(lián)系站長(zhǎng)付費(fèi)協(xié)助。

有問(wèn)題可以加入技術(shù)QQ群一起交流學(xué)習(xí)
本站vip會(huì)員 請(qǐng)加入無(wú)憂模板網(wǎng) VIP群(50604020) PS:加入時(shí)備注用戶名或昵稱(chēng)
普通注冊(cè)會(huì)員或訪客 請(qǐng)加入無(wú)憂模板網(wǎng) 技術(shù)交流群(50604130)
客服微信號(hào):15898888535
聲明:本站所有文章資源內(nèi)容,如無(wú)特殊說(shuō)明或標(biāo)注,均為采集網(wǎng)絡(luò)資源。如若內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系站長(zhǎng)刪除。