getCurrentPages.ts 1.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { getCurrentPage } from '@dcloudio/uni-core'
fxy060608's avatar
fxy060608 已提交
2
import { formatLog } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
import { ComponentPublicInstance } from 'vue'

const pages: ComponentPublicInstance[] = []

export function addCurrentPage(page: ComponentPublicInstance) {
  pages.push(page)
}

export function getCurrentPages() {
  const curPages: ComponentPublicInstance[] = []
  pages.forEach((page) => {
    if (page.__isTabBar) {
      if (page.$.__isActive) {
        curPages.push(page)
      }
    } else {
      curPages.push(page)
    }
  })
  return curPages
}
fxy060608's avatar
fxy060608 已提交
24

fxy060608's avatar
fxy060608 已提交
25
export function removeCurrentPage() {
fxy060608's avatar
fxy060608 已提交
26 27 28 29 30
  const page = getCurrentPage() as ComponentPublicInstance
  if (!page) {
    return
  }
  removePage(page)
fxy060608's avatar
fxy060608 已提交
31 32
}

fxy060608's avatar
fxy060608 已提交
33 34 35
export function removePage(
  curPage: ComponentPublicInstance | Page.PageInstance
) {
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40
  const index = pages.findIndex((page) => page === curPage)
  if (index === -1) {
    return
  }
  if (!curPage.$page.meta.isNVue) {
fxy060608's avatar
fxy060608 已提交
41
    ;(curPage as ComponentPublicInstance).$.appContext.app.unmount()
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46 47
  }
  pages.splice(index, 1)
  if (__DEV__) {
    console.log(formatLog('removePage', curPage.$page))
  }
}