define.ts 1.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { once, PageNodeOptions } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
2
import { createApp, DefineComponent } from 'vue'
fxy060608's avatar
fxy060608 已提交
3
import { createPageNode } from '../dom/Page'
fxy060608's avatar
fxy060608 已提交
4
import { setupPage } from './setup'
fxy060608's avatar
fxy060608 已提交
5
import __vuePlugin from '../plugin'
fxy060608's avatar
fxy060608 已提交
6

fxy060608's avatar
fxy060608 已提交
7
export type VuePageComponent = DefineComponent<PageProps>
fxy060608's avatar
fxy060608 已提交
8 9 10

const pagesMap = new Map<string, ReturnType<typeof createFactory>>()

fxy060608's avatar
fxy060608 已提交
11
export function definePage(pagePath: string, component: VuePageComponent) {
fxy060608's avatar
fxy060608 已提交
12 13 14
  pagesMap.set(pagePath, once(createFactory(component)))
}

fxy060608's avatar
fxy060608 已提交
15 16 17 18 19
interface PageProps {
  __pageId: number
  __pagePath: string
  __pageQuery: Record<string, any>
  __pageInstance: Page.PageInstance['$page']
fxy060608's avatar
fxy060608 已提交
20 21
}

fxy060608's avatar
fxy060608 已提交
22
export function createPage(
fxy060608's avatar
fxy060608 已提交
23 24 25 26
  __pageId: number,
  __pagePath: string,
  __pageQuery: Record<string, any>,
  __pageInstance: Page.PageInstance['$page'],
fxy060608's avatar
fxy060608 已提交
27 28
  pageOptions: PageNodeOptions
) {
fxy060608's avatar
fxy060608 已提交
29
  const pageNode = createPageNode(__pageId, pageOptions, true)
fxy060608's avatar
fxy060608 已提交
30
  // TODO 需要同步 main.js 中开发者设置的plugin,mixin,config等
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36 37 38 39 40 41 42
  const app = createApp(pagesMap.get(__pagePath)!(), {
    __pageId,
    __pagePath,
    __pageQuery,
    __pageInstance,
  }).use(__vuePlugin)
  const oldUnmount = app.unmount
  app.unmount = () => {
    pageNode.isUnmounted = true
    return oldUnmount.call(app)
  }
  return app.mount(pageNode as unknown as Element)
fxy060608's avatar
fxy060608 已提交
43 44
}

fxy060608's avatar
fxy060608 已提交
45 46 47
function createFactory(component: VuePageComponent) {
  return () => {
    return setupPage(component)
fxy060608's avatar
fxy060608 已提交
48 49
  }
}