pageLifetimes.ts 1.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import { extend } from '@vue/shared'

import {
  MPComponentInstance,
5
  CreateLifetimesOptions,
fxy060608's avatar
fxy060608 已提交
6 7 8 9 10 11
} from '@dcloudio/uni-mp-core'

import { $destroyComponent } from '@dcloudio/uni-mp-core'

import { initLifetimes as initComponentLifetimes } from './componentLifetimes'
import { instances } from './parseComponentOptions'
fxy060608's avatar
fxy060608 已提交
12
import { ON_READY } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18 19 20

export function initLifetimes(lifetimesOptions: CreateLifetimesOptions) {
  return extend(initComponentLifetimes(lifetimesOptions), {
    ready(this: MPComponentInstance) {
      if (this.$vm && lifetimesOptions.isPage(this)) {
        if (__PLATFORM__ === 'quickapp-webview' && this.pageinstance) {
          this.__webviewId__ = (this.pageinstance as any).__pageId__
        }
21
        this.$vm.$callCreatedHook()
fxy060608's avatar
fxy060608 已提交
22
        this.$vm.$callHook('mounted')
fxy060608's avatar
fxy060608 已提交
23
        this.$vm.$callHook(ON_READY)
fxy060608's avatar
fxy060608 已提交
24 25 26 27 28 29 30 31 32
      } else {
        this.is && console.warn(this.is + ' is not ready')
      }
    },
    detached(this: MPComponentInstance) {
      this.$vm && $destroyComponent(this.$vm)
      // 清理
      const webviewId = this.__webviewId__
      webviewId &&
33
        Object.keys(instances).forEach((key) => {
fxy060608's avatar
fxy060608 已提交
34 35 36 37
          if (key.indexOf(webviewId + '_') === 0) {
            delete instances[key]
          }
        })
38
    },
fxy060608's avatar
fxy060608 已提交
39 40
  })
}