page.js 2.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
import {
  initWebview,
  createWebview
fxy060608's avatar
init v3  
fxy060608 已提交
4 5 6
} from './webview'

import {
fxy060608's avatar
fxy060608 已提交
7
  navigateFinish
fxy060608's avatar
init v3  
fxy060608 已提交
8 9
} from './navigator'

Q
qiang 已提交
10 11
import tabBar from '../framework/tab-bar'

fxy060608's avatar
fxy060608 已提交
12 13 14 15
import {
  createPage
} from '../../page-factory'

fxy060608's avatar
fxy060608 已提交
16
const pages = []
fxy060608's avatar
fxy060608 已提交
17

fxy060608's avatar
fxy060608 已提交
18 19 20 21
export function getCurrentPages (returnAll) {
  return returnAll ? pages.slice(0) : pages.filter(page => {
    return !page.$page.meta.isTabBar || page.$page.meta.visible
  })
fxy060608's avatar
fxy060608 已提交
22
}
fxy060608's avatar
fxy060608 已提交
23

fxy060608's avatar
fxy060608 已提交
24 25 26
/**
 * 首页需要主动registerPage,二级页面路由跳转时registerPage
 */
fxy060608's avatar
fxy060608 已提交
27 28
export function registerPage ({
  path,
fxy060608's avatar
fxy060608 已提交
29
  query,
fxy060608's avatar
fxy060608 已提交
30
  openType,
fxy060608's avatar
fxy060608 已提交
31
  webview
fxy060608's avatar
fxy060608 已提交
32 33
}) {
  const routeOptions = JSON.parse(JSON.stringify(__uniRoutes.find(route => route.path === path)))
fxy060608's avatar
fxy060608 已提交
34

fxy060608's avatar
fxy060608 已提交
35 36 37
  if (openType === 'reLaunch' || pages.length === 0) {
    // pages.length===0 表示首页触发 redirectTo
    routeOptions.meta.isQuit = true
Q
qiang 已提交
38 39
  } else if (!routeOptions.meta.isTabBar) {
    routeOptions.meta.isQuit = false
fxy060608's avatar
fxy060608 已提交
40 41
  }

fxy060608's avatar
fxy060608 已提交
42
  if (!webview) {
fxy060608's avatar
fxy060608 已提交
43
    webview = createWebview(path, routeOptions)
44
  } else {
45 46
    webview = plus.webview.getWebviewById(webview.id)
    webview.nvue = routeOptions.meta.isNVue
fxy060608's avatar
fxy060608 已提交
47 48
  }

fxy060608's avatar
fxy060608 已提交
49 50 51 52 53
  if (routeOptions.meta.isTabBar) {
    routeOptions.meta.visible = true
  }

  if (routeOptions.meta.isTabBar && webview.id !== '1') {
Q
qiang 已提交
54
    tabBar.append(webview)
fxy060608's avatar
fxy060608 已提交
55 56
  }

fxy060608's avatar
fxy060608 已提交
57 58 59
  if (process.env.NODE_ENV !== 'production') {
    console.log(`[uni-app] registerPage`, path, webview.id)
  }
fxy060608's avatar
fxy060608 已提交
60

fxy060608's avatar
init v3  
fxy060608 已提交
61
  initWebview(webview, routeOptions)
fxy060608's avatar
fxy060608 已提交
62 63

  const route = path.slice(1)
fxy060608's avatar
fxy060608 已提交
64

fxy060608's avatar
fxy060608 已提交
65
  webview.__uniapp_route = route
fxy060608's avatar
fxy060608 已提交
66

fxy060608's avatar
init v3  
fxy060608 已提交
67
  const pageInstance = {
fxy060608's avatar
fxy060608 已提交
68
    route,
fxy060608's avatar
fxy060608 已提交
69
    options: Object.assign({}, query || {}),
fxy060608's avatar
fxy060608 已提交
70 71 72
    $getAppWebview () {
      return webview
    },
fxy060608's avatar
fxy060608 已提交
73 74 75 76
    $page: {
      id: parseInt(webview.id),
      meta: routeOptions.meta,
      path,
fxy060608's avatar
fxy060608 已提交
77 78 79 80 81 82 83
      route,
      openType
    },
    $remove () {
      const index = pages.findIndex(page => page === this)
      if (index !== -1) {
        pages.splice(index, 1)
fxy060608's avatar
fxy060608 已提交
84 85 86
        if (!webview.nvue) {
          this.$vm.$destroy()
        }
fxy060608's avatar
fxy060608 已提交
87 88 89 90
        if (process.env.NODE_ENV !== 'production') {
          console.log(`[uni-app] removePage`, path, webview.id)
        }
      }
fxy060608's avatar
fxy060608 已提交
91
    }
fxy060608's avatar
init v3  
fxy060608 已提交
92 93 94 95 96
  }

  pages.push(pageInstance)

  // 首页是 nvue 时,在 registerPage 时,执行路由堆栈
97
  if (webview.id === '1' && webview.nvue) {
fxy060608's avatar
fxy060608 已提交
98 99
    __uniConfig.onReady(function () {
      navigateFinish(webview)
fxy060608's avatar
init v3  
fxy060608 已提交
100 101 102 103 104
    })
  }

  if (__PLATFORM__ === 'app-plus') {
    if (!webview.nvue) {
fxy060608's avatar
fxy060608 已提交
105
      const pageId = webview.id
fxy060608's avatar
fxy060608 已提交
106 107
      try {
        createPage(route, pageId, query, pageInstance).$mount()
fxy060608's avatar
fxy060608 已提交
108 109 110
      } catch (e) {
        console.error(e)
      }
fxy060608's avatar
init v3  
fxy060608 已提交
111 112
    }
  }
fxy060608's avatar
fxy060608 已提交
113 114

  return webview
fxy060608's avatar
init v3  
fxy060608 已提交
115
}