'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
  initAppLocale
} from 'uni-helpers/i18n'
 
import initRouterGuard from './router-guard'
 
let appVm = false
 
export function getApp () {
  return appVm
}
 
export function getCurrentPages (isAll = false, ignoreError = false) {
  const pages = []
  const app = getApp()
  if (!app) {
    if (ignoreError) {
      console.error('app is not ready')
    }
    return []
  }
  let childrenVm = app.$children[0]
  if (childrenVm && childrenVm.$children.length) {
    const tabBarVm = childrenVm.$children.find(vm => vm.$options.name === 'TabBar')
    const layoutVm = childrenVm.$children.find(vm => vm.$options.name === 'Layout')
    if (layoutVm) {
      childrenVm = layoutVm
    }
    childrenVm.$children.forEach(vm => {
      if (tabBarVm !== vm && vm.$children.length && vm.$children[0].$options.name === 'Page' && vm.$children[0]
        .$slots
        .page) {
        // vm.$children[0]=Page->PageBody->RealPage
        const pageBody = vm.$children[0].$children.find(vm => vm.$options.name === 'PageBody')
        const pageVm = pageBody && pageBody.$children.find(vm => !!vm.$page)
        if (pageVm) {
          let isActive = true
          if (!isAll && tabBarVm && pageVm.$page && pageVm.$page.meta.isTabBar) { // 选项卡仅列出活动的
            if (app.$route.meta && app.$route.meta.isTabBar) { // 当前页面路由是 tabBar
              if (app.$route.path !== pageVm.$page.path) {
                isActive = false
              }
            } else {
              if (tabBarVm.__path__ !== pageVm.$page.path) {
                isActive = false
              }
            }
          }
          if (isActive) {
            pages.push(pageVm)
          }
        } else {
          // TODO
          // console.error('pageVm is undefined')
        }
      }
    })
  }
  // 当页面返回过程中,请求 getCurrentPages 时,可能会获取到前一个已经准备销毁的 page
  const length = pages.length
  if (length > 1) {
    const currentPage = pages[length - 1]
    if (currentPage.$page.path !== app.$route.path) { // 删除已经准备销毁的上个页面
      pages.splice(length - 1, 1)
    }
  }
 
  return pages
}
 
export default function createApp (Vue, vm, routes) {
  appVm = vm
  appVm.$vm = vm
  appVm.globalData = appVm.$options.globalData || {}
  initAppLocale(Vue, appVm)
  // initEvents(appVm)
  initRouterGuard(appVm, routes)
}