# getApp()

getApp() 函数用于获取当前应用实例,可通过应用实例调用 App.uvue methods 中定义的方法, 详见

  • HBuilderX 4.31以前,getApp返回的是vue实例。并且在uts插件中无法使用。
  • HBuilderX 4.31+,新增了UniApp对象,用于管理app,getApp返回的是UniApp对象。而vue实例,则是UniApp对象的一个vm属性。

UniApp对象可以在uts插件和uvue页面中同时使用,但vm属性以及相关的globalData仍然只能在uvue页面中才能使用。

# getApp 兼容性

Web 微信小程序 Android iOS iOS uni-app x UTS 插件 HarmonyOS
4.0 4.31 4.61

# 返回值

类型
UniApp
名称 类型 必备 默认值 兼容性 描述
vm ComponentPublicInstance -
App vue 实例对象
globalData any -
全局对象
$vm ComponentPublicInstance -
App vue 实例对象 已废弃,仅为了向下兼容保留

# UniApp 的方法

# getAndroidApplication(): Application

getAndroidApplication 获取 Android 应用 Application 上下文

# getAndroidApplication 兼容性
Web 微信小程序 Android iOS HarmonyOS
x x 4.31 x x
# 返回值
类型
Application

# getHarmonyAbility(): UIAbility

getHarmonyAbility 获取 鸿蒙应用 Ability 实例

# getHarmonyAbility 兼容性
Web 微信小程序 Android iOS HarmonyOS
x x x x 4.61
# 返回值
类型
UIAbility

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

扫码体验(手机浏览器跳转到App直达页)

示例

<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1; padding-bottom: 20px">
  <!-- #endif -->
    <view>
      <page-head title="getApp"></page-head>
      <view class="uni-padding-wrap">
        <button @click="getGlobalData">get globalData</button>
        <template v-if="originGlobalData.str.length">
          <text class="uni-common-mt bold">初始的 globalData:</text>
          <text class="uni-common-mt">globalData string: {{ originGlobalData.str }}</text>
          <text class="uni-common-mt">globalData number: {{ originGlobalData.num }}</text>
          <text class="uni-common-mt">globalData boolean: {{ originGlobalData.bool }}</text>
          <text class="uni-common-mt">globalData object: {{ originGlobalData.obj }}</text>
          <text class="uni-common-mt">globalData null: {{ originGlobalData.null }}</text>
          <text class="uni-common-mt">globalData array: {{ originGlobalData.arr }}</text>
          <text class="uni-common-mt">globalData Set: {{ originGlobalData.mySet }}</text>
          <text class="uni-common-mt">globalData Map: {{ originGlobalData.myMap }}</text>
          <text class="uni-common-mt">globalData func 返回值: {{ originGlobalDataFuncRes }}</text>
        </template>
        <button @click="setGlobalData" class="uni-common-mt">
          set globalData
        </button>
        <template v-if="newGlobalData.bool">
          <text class="uni-common-mt bold">更新后的 globalData:</text>
          <text class="uni-common-mt">globalData string: {{ newGlobalData.str }}</text>
          <text class="uni-common-mt">globalData number: {{ newGlobalData.num }}</text>
          <text class="uni-common-mt">globalData boolean: {{ newGlobalData.bool }}</text>
          <text class="uni-common-mt">globalData object: {{ newGlobalData.obj }}</text>
          <text class="uni-common-mt">globalData null: {{ newGlobalData.null }}</text>
          <text class="uni-common-mt">globalData array: {{ newGlobalData.arr }}</text>
          <text class="uni-common-mt">globalData Set: {{ newGlobalData.mySet }}</text>
          <text class="uni-common-mt">globalData Map: {{ newGlobalData.myMap }}</text>
          <text class="uni-common-mt">globalData func 返回值: {{ newGlobalDataFuncRes }}</text>
        </template>
        <text class="uni-common-mt">点击按钮调用 App.uvue methods</text>
        <text class="uni-common-mt">increasetLifeCycleNum 方法</text>
        <button class="uni-common-mt" @click="_increasetLifeCycleNum">
          increase lifeCycleNum
        </button>
        <text class="uni-common-mt">lifeCycleNum: {{ lifeCycleNum }}</text>
        <!-- #ifndef MP -->
        <button class="uni-common-mt" @click="getAndroidApplication">
          getAndroidApplication
        </button>
        <text class="uni-common-mt">androidApplication is null: {{ androidApplication == null }}</text>
        <!-- #endif -->
      </view>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script lang="uts">
  import { state, setLifeCycleNum } from '@/store/index.uts'

  type MyGlobalData = {
    str : string,
    num : number,
    bool : boolean,
    obj : UTSJSONObject,
    null : string | null,
    arr : number[],
    mySet : string[],
    myMap : UTSJSONObject,
    func : () => string
  }

  export default {
    data() {
      return {
        originGlobalData: {
          str: '',
          num: 0,
          bool: false,
          obj: {
            str: '',
            num: 0,
            bool: false
          } as UTSJSONObject,
          null: null,
          arr: [] as number[],
          mySet: [] as string[],
          myMap: {},
          func: () : string => ''
        } as MyGlobalData,
        originGlobalDataFuncRes: '',
        newGlobalData: {
          str: '',
          num: 0,
          bool: false,
          obj: {
            str: '',
            num: 0,
            bool: false
          } as UTSJSONObject,
          null: null,
          arr: [] as number[],
          mySet: [] as string[],
          myMap: {},
          func: () : string => ''
        } as MyGlobalData,
        newGlobalDataFuncRes: '',
        lifeCycleNum: 0,
        androidApplication: null as any | null
      }
    },
    onReady() {
      this.lifeCycleNum = state.lifeCycleNum
    },
    methods: {
      getGlobalData() {
        const app = getApp()

        this.originGlobalData.str = app.globalData.str
        this.originGlobalData.num = app.globalData.num
        this.originGlobalData.bool = app.globalData.bool
        this.originGlobalData.obj = app.globalData.obj
        this.originGlobalData.null = app.globalData.null
        this.originGlobalData.arr = app.globalData.arr
        app.globalData.mySet.forEach((value : string) => {
          this.originGlobalData.mySet.push(value)
        })
        app.globalData.myMap.forEach((value : any, key : string) => {
          this.originGlobalData.myMap[key] = value
        })
        this.originGlobalData.func = app.globalData.func
        this.originGlobalDataFuncRes = this.originGlobalData.func()
      },
      setGlobalData() {
        const app = getApp()

        app.globalData.str = 'new globalData str'
        app.globalData.num = 100
        app.globalData.bool = true
        app.globalData.obj = {
          str: 'new globalData obj str',
          num: 200,
          bool: true
        }
        app.globalData.null = 'not null'
        app.globalData.arr = [1, 2, 3]
        app.globalData.mySet = new Set(['a', 'b', 'c'])
        app.globalData.myMap = new Map([
          ['a', 1],
          ['b', 2],
          ['c', 3]
        ])
        app.globalData.func = () : string => {
          return 'new globalData func'
        }

        this.newGlobalData.str = app.globalData.str
        this.newGlobalData.num = app.globalData.num
        this.newGlobalData.bool = app.globalData.bool
        this.newGlobalData.obj = app.globalData.obj
        this.newGlobalData.null = app.globalData.null
        this.newGlobalData.arr = app.globalData.arr
        app.globalData.mySet.forEach((value : string) => {
          this.newGlobalData.mySet.push(value)
        })
        app.globalData.myMap.forEach((value : any, key : string) => {
          this.newGlobalData.myMap[key] = value
        })
        this.newGlobalData.func = app.globalData.func
        this.newGlobalDataFuncRes = this.newGlobalData.func()
      },
      _increasetLifeCycleNum: function () {
        const app = getApp()
        app.vm!.increasetLifeCycleNum()
        this.lifeCycleNum = state.lifeCycleNum
      },
      // 自动化测试
      setLifeCycleNum(num : number) {
        setLifeCycleNum(num)
      },
      // #ifndef MP
      getAndroidApplication() : boolean {
        const app = getApp()
        this.androidApplication = app.getAndroidApplication()
        return this.androidApplication !== null
      }
      // #endif
    },
  }
</script>

<style>
  .bold {
    font-weight: bold;
  }

  .hr {
    border-bottom: 1px solid #ccc;
  }
</style>

# 全局方法调用

以上示例,getApp()后调用了app.uvue里定义的increasetLifeCycleNum方法。app.uvue的源码另见

调整 :HBuilderX 4.31 getApp() 返回值调整为 UniApp 类型,调用 App.uvue 中定义的全局方法,需要由 getApp().methodName() 调整为 getApp().vm?.methodName()

# 参见

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息