Back to Uni App

Launch

docs/api/launch.md

2.3.316.8 KB
Original Source
<!-- ## uni.getLaunchOptionsSync() @getlaunchoptionssync -->

::: sourceCode

uni.getLaunchOptionsSync() @getlaunchoptionssync

GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-getLaunchOptionsSync

GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-getLaunchOptionsSync

:::

获取首次启动时的参数。返回值与App.onLaunch的回调参数一致

getLaunchOptionsSync 兼容性

Web微信小程序AndroidiOSHarmonyOSHarmonyOS(Vapor)
4.04.413.914.114.61<a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a>

返回值

类型
OnLaunchOptions

OnLaunchOptions 的属性描述

名称类型必备默认值兼容性描述
pathstring-Web: 4.0; 微信小程序: 4.41; Android: 3.91; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): x首次启动时的页面路径。返回值与App.onLaunch的回调参数一致
appSchemestring-Web: x; 微信小程序: x; Android: 4.25; iOS: 4.25; HarmonyOS: 4.81; HarmonyOS(Vapor): x首次启动时的Scheme。返回值与App.onLaunch的回调参数一致
appLinkstring-Web: x; 微信小程序: x; Android: x; iOS: 4.25; HarmonyOS: 4.81; HarmonyOS(Vapor): x首次启动时的appLink。返回值与App.onLaunch的回调参数一致
queryUTSJSONObject-Web: 4.0; 微信小程序: 4.41; Android: √; iOS: √; HarmonyOS: 4.81; HarmonyOS(Vapor): x启动时的 query 参数
apiCategorystring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -需要基础库: 2.20.0

API 类别

可选值:

  • 'default': 默认类别;
  • 'nativeFunctionalized': 原生功能化,视频号直播商品、商品橱窗等场景打开的小程序;
  • 'browseOnly': 仅浏览,朋友圈快照页等场景打开的小程序;
  • 'embedded': 内嵌,通过打开半屏小程序能力打开的小程序;
  • 'chatTool': 聊天工具,通过打开聊天工具能力打开的小程序; | | forwardMaterials | any | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 打开的文件信息数组,只有从聊天素材场景打开(scene为1173)才会携带该参数 | | referrerInfo | OnLaunchOptionsReferrerInfo | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 来源信息。从另一个小程序、公众号或 App 进入小程序时返回。否则返回 {}。(参见后文注意) | | scene | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | | | chatType | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 从微信群聊/单聊打开小程序时,chatType 表示具体微信群聊/单聊类型

可选值:

  • 1: 微信联系人单聊;
  • 2: 企业微信联系人单聊;
  • 3: 普通微信群聊;
  • 4: 企业微信互通群聊; | | shareTicket | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | |
apiCategory 的属性描述
合法值兼容性描述
defaultWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
nativeFunctionalizedWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
browseOnlyWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
embeddedWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
chatToolWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
referrerInfo 的属性描述
名称类型必备默认值兼容性描述
appIdstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -来源小程序、公众号或 App 的 appId
extraDataany-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -来源小程序传过来的数据,scene=1037或1038时支持
chatType 的属性描述
合法值兼容性描述
1Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
2Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
3Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
4Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
  • 如果应用通过scheme或applink(通用链接)启动,可以通过本API获取相应参数。配置scheme或applink需在AndroidManifest.xml或info.plist中配置,打包后生效。如开发直达页面功能,一般在应用的onShow生命周期监听。详见

示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/get-launch-options-sync/get-launch-options-sync

appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/get-launch-options-sync/get-launch-options-sync

示例

vue
<template>
  <page-head title="getLaunchOptionsSync"></page-head>
  <view class="uni-padding-wrap">
    <button @click="getLaunchOptionsSync">getLaunchOptionsSync</button>
    <view class="uni-common-mt">
      <text>应用本次启动路径:</text>
      <text style="margin-top: 5px">{{ data.launchOptionsPath }}</text>
    </view>
    <view class="uni-common-mt">
      <text>应用本次启动:</text>
      <text style="margin-top: 5px">{{ data.launchOptionsString }}</text>
    </view>
  </view>
</template>

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

  type DataType = {
    checked: boolean;
    homePagePath: string;
    launchOptionsPath: string;
    launchOptionsString: string;
    testResult: boolean;
  }

  const data = reactive({
    checked: false,
    // #ifdef VUE3-VAPOR
    homePagePath: 'pages/tabBar/tab-bar',
    // #endif
    // #ifndef VUE3-VAPOR
    homePagePath: 'pages/tabBar/component',
    // #endif
    launchOptionsPath: '',
    launchOptionsString: '',
    testResult: false
  } as DataType)

  const compareOnLaunchRes = () => {
    const launchOptions = uni.getLaunchOptionsSync();
    data.launchOptionsString = JSON.stringify(launchOptions, null, 2)

    const appLaunchOptions = state.globalData.launchOptions

    const isPathSame = launchOptions.path == appLaunchOptions.path
    const isAppSchemeSame = launchOptions.appScheme == appLaunchOptions.appScheme
    const isAppLinkSame = launchOptions.appLink == appLaunchOptions.appLink
    data.testResult = isPathSame && isAppSchemeSame && isAppLinkSame
  }

  const getLaunchOptionsSync = () => {
    const launchOptions = uni.getLaunchOptionsSync()
    data.launchOptionsPath = launchOptions.path
    if (launchOptions.path == data.homePagePath) {
      data.checked = true
    }
  }

  onReady(() => {
    compareOnLaunchRes()
  })

  defineExpose({
    data,
    getLaunchOptionsSync
  })
</script>

:::

参见

<!-- ## uni.getEnterOptionsSync() @getenteroptionssync -->

::: sourceCode

uni.getEnterOptionsSync() @getenteroptionssync

GitCode: https://gitcode.com/dcloud/uni-api/tree/alpha/uni_modules/uni-getEnterOptionsSync

GitHub: https://github.com/dcloudio/uni-api/tree/alpha/uni_modules/uni-getEnterOptionsSync

:::

获取本次启动时的参数。返回值与App.onShow的回调参数一致

uni.getEnterOptionsSync 和 uni.getLaunchOptionsSync 的区别,相当于应用的 onShow 和 onLaunch 的区别,详见应用生命周期

getEnterOptionsSync 兼容性

Web微信小程序AndroidiOSHarmonyOSHarmonyOS(Vapor)
4.04.414.254.254.61<a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a>

返回值

类型
OnShowOptions

OnShowOptions 的属性描述

名称类型必备默认值兼容性描述
pathstring-Web: 4.0; 微信小程序: 4.41; Android: 4.25; iOS: 4.25; HarmonyOS: 4.61; HarmonyOS(Vapor): x本次启动时页面的路径
appSchemestring-Web: x; 微信小程序: x; Android: 4.25; iOS: 4.25; HarmonyOS: 4.81; HarmonyOS(Vapor): x本次启动时的Scheme。返回值与App.onShow的回调参数一致
appLinkstring-Web: x; 微信小程序: x; Android: x; iOS: 4.25; HarmonyOS: 4.81; HarmonyOS(Vapor): x本次启动时的appLink。返回值与App.onShow的回调参数一致
queryUTSJSONObject-Web: 4.0; 微信小程序: 4.41; Android: √; iOS: √; HarmonyOS: 4.81; HarmonyOS(Vapor): x启动时的 query 参数
apiCategorystring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -需要基础库: 2.20.0

API 类别

可选值:

  • 'default': 默认类别;
  • 'nativeFunctionalized': 原生功能化,视频号直播商品、商品橱窗等场景打开的小程序;
  • 'browseOnly': 仅浏览,朋友圈快照页等场景打开的小程序;
  • 'embedded': 内嵌,通过打开半屏小程序能力打开的小程序;
  • 'chatTool': 聊天工具,通过打开聊天工具能力打开的小程序; | | forwardMaterials | any | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 打开的文件信息数组,只有从聊天素材场景打开(scene为1173)才会携带该参数 | | referrerInfo | OnShowOptionsReferrerInfo | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 来源信息。从另一个小程序、公众号或 App 进入小程序时返回。否则返回 {}。(参见后文注意) | | scene | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | | | chatType | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 从微信群聊/单聊打开小程序时,chatType 表示具体微信群聊/单聊类型

可选值:

  • 1: 微信联系人单聊;
  • 2: 企业微信联系人单聊;
  • 3: 普通微信群聊;
  • 4: 企业微信互通群聊; | | shareTicket | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | |
apiCategory 的属性描述
合法值兼容性描述
defaultWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
nativeFunctionalizedWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
browseOnlyWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
embeddedWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
chatToolWeb: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
referrerInfo 的属性描述
名称类型必备默认值兼容性描述
appIdstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -来源小程序、公众号或 App 的 appId
extraDataany-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -来源小程序传过来的数据,scene=1037或1038时支持
chatType 的属性描述
合法值兼容性描述
1Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
2Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
3Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
4Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: --
  • 如果应用通过scheme或applink(通用链接)启动或从后台激活到前台,可以通过本API获取相应参数。配置scheme或applink需在AndroidManifest.xml或info.plist中配置,打包后生效。如开发直达页面功能,一般在应用的onShow生命周期监听。详见

示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见 ::: preview https://hellouniappx.dcloud.net.cn/web/#/pages/API/get-enter-options-sync/get-enter-options-sync

appRedirect https://hellouniappx.dcloud.net.cn/appredirect.html?path=pages/API/get-enter-options-sync/get-enter-options-sync

示例

vue
<template>
  <page-head title="getEnterOptionsSync"></page-head>
  <view class="uni-padding-wrap">
    <view class="uni-common-mt">
      <text>应用本次启动路径:</text>
      <text style="margin-top: 5px">{{ data.enterOptionsString }}</text>
    </view>
  </view>
</template>

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

type DataType = {
  enterOptionsString: string,
  testResult: boolean,
}

// Data
const data = reactive({
  enterOptionsString: '',
  testResult: false,
} as DataType)

// Lifecycle
onReady(() => {
  const appShowOptions = state.globalData.showOptions
  const enterOptions = uni.getEnterOptionsSync()
  data.enterOptionsString = JSON.stringify(enterOptions, null, 2)
  data.testResult = (enterOptions.path == appShowOptions.path && enterOptions.appScheme == appShowOptions.appScheme && enterOptions.appLink == appShowOptions.appLink)
})

defineExpose({
  data
})
</script>

:::

参见

<!-- UTSAPIJSON.launch.example -->

通用类型

GeneralCallbackResult

名称类型必备默认值兼容性描述
errMsgstring-Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: -错误信息