# uni.getLaunchOptionsSync() GitCodeGitHub

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

# getLaunchOptionsSync 兼容性

Web 微信小程序 Android iOS HarmonyOS
4.0 4.41 3.91 4.11 4.61

# 返回值

类型
OnLaunchOptions
名称 类型 必备 默认值 兼容性 描述
path string -
首次启动时的页面路径。返回值与App.onLaunch的回调参数一致
appScheme string -
首次启动时的Scheme。返回值与App.onLaunch的回调参数一致
appLink string -
首次启动时的appLink。返回值与App.onLaunch的回调参数一致
query any -
启动时的 query 参数
apiCategory string -
需要基础库: 2.20.0

API 类别

可选值:
- 'default': 默认类别;
- 'nativeFunctionalized': 原生功能化,视频号直播商品、商品橱窗等场景打开的小程序;
- 'browseOnly': 仅浏览,朋友圈快照页等场景打开的小程序;
- 'embedded': 内嵌,通过打开半屏小程序能力打开的小程序;
- 'chatTool': 聊天工具,通过打开聊天工具能力打开的小程序;
合法值 兼容性 描述
default
-
-
nativeFunctionalized
-
-
browseOnly
-
-
embedded
-
-
chatTool
-
-
forwardMaterials any -
打开的文件信息数组,只有从聊天素材场景打开(scene为1173)才会携带该参数
referrerInfo OnLaunchOptionsReferrerInfo -
来源信息。从另一个小程序、公众号或 App 进入小程序时返回。否则返回 {}。(参见后文注意)
名称 类型 必备 默认值 兼容性 描述
appId string -
来源小程序、公众号或 App 的 appId
extraData any -
来源小程序传过来的数据,scene=1037或1038时支持
scene number -
chatType number -
从微信群聊/单聊打开小程序时,chatType 表示具体微信群聊/单聊类型

可选值:
- 1: 微信联系人单聊;
- 2: 企业微信联系人单聊;
- 3: 普通微信群聊;
- 4: 企业微信互通群聊;
合法值 兼容性 描述
1
-
-
2
-
-
3
-
-
4
-
-
shareTicket string -
  • 如果应用通过scheme或applink(通用链接)启动,可以通过本API获取相应参数。配置scheme或applink需在AndroidManifest.xml或info.plist中配置,打包后生效。如开发直达页面功能,一般在应用的onShow生命周期监听。详见

# 示例

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

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

示例

<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">{{ launchOptionsPath }}</text>
    </view>
    <view class="uni-common-mt">
      <text>应用本次启动:</text>
      <text style="margin-top: 5px">{{ launchOptionsString }}</text>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        checked: false,
        homePagePath: 'pages/tabBar/component',
        launchOptionsPath: '',
        launchOptionsString: '',
        testResult: false
      }
    },
    onReady() {
      this.compareOnLaunchRes()
    },
    methods: {
      compareOnLaunchRes() {
        const launchOptions = uni.getLaunchOptionsSync();
        this.launchOptionsString = JSON.stringify(launchOptions, null, 2)

        const app = getApp()
        const appOnLaunch = app.globalData.launchOptions

        const isPathSame = launchOptions.path == appOnLaunch.path
        const isAppSchemeSame = launchOptions.appScheme == appOnLaunch.appScheme
        const isAppLinkSame = launchOptions.appLink == appOnLaunch.appLink
        this.testResult = isPathSame && isAppSchemeSame && isAppLinkSame
      },

      getLaunchOptionsSync() {
        const launchOptions = uni.getLaunchOptionsSync()
        this.launchOptionsPath = launchOptions.path
        if (launchOptions.path == this.homePagePath) {
          this.checked = true
        }
      },
    },
  }
</script>

# 参见

# uni.getEnterOptionsSync() GitCodeGitHub

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

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

# getEnterOptionsSync 兼容性

Web 微信小程序 Android iOS HarmonyOS
4.0 4.41 4.25 4.25 4.61

# 返回值

类型
OnShowOptions
名称 类型 必备 默认值 兼容性 描述
path string -
本次启动时页面的路径
appScheme string -
本次启动时的Scheme。返回值与App.onShow的回调参数一致
appLink string -
本次启动时的appLink。返回值与App.onShow的回调参数一致
query any -
启动时的 query 参数
apiCategory string -
需要基础库: 2.20.0

API 类别

可选值:
- 'default': 默认类别;
- 'nativeFunctionalized': 原生功能化,视频号直播商品、商品橱窗等场景打开的小程序;
- 'browseOnly': 仅浏览,朋友圈快照页等场景打开的小程序;
- 'embedded': 内嵌,通过打开半屏小程序能力打开的小程序;
- 'chatTool': 聊天工具,通过打开聊天工具能力打开的小程序;
合法值 兼容性 描述
default
-
-
nativeFunctionalized
-
-
browseOnly
-
-
embedded
-
-
chatTool
-
-
forwardMaterials any -
打开的文件信息数组,只有从聊天素材场景打开(scene为1173)才会携带该参数
referrerInfo OnShowOptionsReferrerInfo -
来源信息。从另一个小程序、公众号或 App 进入小程序时返回。否则返回 {}。(参见后文注意)
名称 类型 必备 默认值 兼容性 描述
appId string -
来源小程序、公众号或 App 的 appId
extraData any -
来源小程序传过来的数据,scene=1037或1038时支持
scene number -
chatType number -
从微信群聊/单聊打开小程序时,chatType 表示具体微信群聊/单聊类型

可选值:
- 1: 微信联系人单聊;
- 2: 企业微信联系人单聊;
- 3: 普通微信群聊;
- 4: 企业微信互通群聊;
合法值 兼容性 描述
1
-
-
2
-
-
3
-
-
4
-
-
shareTicket string -
  • 如果应用通过scheme或applink(通用链接)启动或从后台激活到前台,可以通过本API获取相应参数。配置scheme或applink需在AndroidManifest.xml或info.plist中配置,打包后生效。如开发直达页面功能,一般在应用的onShow生命周期监听。详见

# 示例

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

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

示例

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

<script>
  export default {
    data() {
      return {
        enterOptionsString: '',
        testResult: false
      }
    },
    onReady() {
      const app = getApp()
      const appOnShow = app.globalData.onShowOption
      const onShowOption = uni.getEnterOptionsSync()
      this.enterOptionsString = JSON.stringify(onShowOption, null, 2)
      this.testResult = (onShowOption.path == appOnShow.path && onShowOption.appScheme == appOnShow.appScheme && onShowOption.appLink == appOnShow.appLink)
    }
  }
</script>

# 参见

# 通用类型

# GeneralCallbackResult

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