page.js 3.1 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 {
fxy060608's avatar
fxy060608 已提交
45
    webview = plus.webview.getWebviewById(webview.id)
46
    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
    $getAppWebview () {
      // 重要,不能直接返回 webview 对象,因为 plus 可能会被二次替换,返回的 webview 对象内部的 plus 不正确
fxy060608's avatar
fxy060608 已提交
72 73
      // 导致 webview.getStyle 等逻辑出错(旧的 webview 内部 plus 被释放)
      return plus.webview.getWebviewById(webview.id)
fxy060608's avatar
fxy060608 已提交
74
    },
fxy060608's avatar
fxy060608 已提交
75 76 77 78
    $page: {
      id: parseInt(webview.id),
      meta: routeOptions.meta,
      path,
fxy060608's avatar
fxy060608 已提交
79 80 81 82 83 84 85
      route,
      openType
    },
    $remove () {
      const index = pages.findIndex(page => page === this)
      if (index !== -1) {
        pages.splice(index, 1)
fxy060608's avatar
fxy060608 已提交
86 87 88
        if (!webview.nvue) {
          this.$vm.$destroy()
        }
fxy060608's avatar
fxy060608 已提交
89 90 91 92
        if (process.env.NODE_ENV !== 'production') {
          console.log(`[uni-app] removePage`, path, webview.id)
        }
      }
fxy060608's avatar
fxy060608 已提交
93 94 95 96 97
    },
    // 兼容小程序框架
    selectComponent (selector) {
      return this.$vm.selectComponent(selector)
    },
fxy060608's avatar
fxy060608 已提交
98 99
    selectAllComponents (selector) {
      return this.$vm.selectAllComponents(selector)
fxy060608's avatar
fxy060608 已提交
100
    }
fxy060608's avatar
init v3  
fxy060608 已提交
101 102 103 104 105
  }

  pages.push(pageInstance)

  // 首页是 nvue 时,在 registerPage 时,执行路由堆栈
106
  if (webview.id === '1' && webview.nvue) {
fxy060608's avatar
fxy060608 已提交
107 108
    __uniConfig.onReady(function () {
      navigateFinish(webview)
fxy060608's avatar
init v3  
fxy060608 已提交
109 110 111 112 113
    })
  }

  if (__PLATFORM__ === 'app-plus') {
    if (!webview.nvue) {
fxy060608's avatar
fxy060608 已提交
114
      const pageId = webview.id
fxy060608's avatar
fxy060608 已提交
115 116
      try {
        createPage(route, pageId, query, pageInstance).$mount()
fxy060608's avatar
fxy060608 已提交
117 118 119
      } catch (e) {
        console.error(e)
      }
fxy060608's avatar
init v3  
fxy060608 已提交
120 121
    }
  }
fxy060608's avatar
fxy060608 已提交
122 123

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