docs/api/launch.md
::: sourceCode
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的回调参数一致
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.91 | 4.11 | 4.61 | <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> |
| 类型 |
|---|
| OnLaunchOptions |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| path | string | 是 | - | Web: 4.0; 微信小程序: 4.41; Android: 3.91; iOS: 4.11; HarmonyOS: 4.61; HarmonyOS(Vapor): x | 首次启动时的页面路径。返回值与App.onLaunch的回调参数一致 |
| appScheme | string | 否 | - | Web: x; 微信小程序: x; Android: 4.25; iOS: 4.25; HarmonyOS: 4.81; HarmonyOS(Vapor): x | 首次启动时的Scheme。返回值与App.onLaunch的回调参数一致 |
| appLink | string | 否 | - | Web: x; 微信小程序: x; Android: x; iOS: 4.25; HarmonyOS: 4.81; HarmonyOS(Vapor): x | 首次启动时的appLink。返回值与App.onLaunch的回调参数一致 |
| query | UTSJSONObject | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: √; iOS: √; HarmonyOS: 4.81; HarmonyOS(Vapor): x | 启动时的 query 参数 |
| apiCategory | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 需要基础库: 2.20.0 |
API 类别
可选值:
{}。(参见后文注意)
|
| scene | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | |
| chatType | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 从微信群聊/单聊打开小程序时,chatType 表示具体微信群聊/单聊类型可选值:
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| default | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| nativeFunctionalized | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| browseOnly | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| embedded | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| chatTool | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| appId | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 来源小程序、公众号或 App 的 appId |
| extraData | any | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 来源小程序传过来的数据,scene=1037或1038时支持 |
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| 1 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 2 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 3 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 4 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
示例为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
示例
<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>
:::
::: sourceCode
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 的区别,详见应用生命周期
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 4.25 | 4.25 | 4.61 | <a style="color:unset;" href="https://vote.dcloud.net.cn/#/?name=uni-app%20x">x</a> |
| 类型 |
|---|
| OnShowOptions |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| path | string | 是 | - | Web: 4.0; 微信小程序: 4.41; Android: 4.25; iOS: 4.25; HarmonyOS: 4.61; HarmonyOS(Vapor): x | 本次启动时页面的路径 |
| appScheme | string | 否 | - | Web: x; 微信小程序: x; Android: 4.25; iOS: 4.25; HarmonyOS: 4.81; HarmonyOS(Vapor): x | 本次启动时的Scheme。返回值与App.onShow的回调参数一致 |
| appLink | string | 否 | - | Web: x; 微信小程序: x; Android: x; iOS: 4.25; HarmonyOS: 4.81; HarmonyOS(Vapor): x | 本次启动时的appLink。返回值与App.onShow的回调参数一致 |
| query | UTSJSONObject | 否 | - | Web: 4.0; 微信小程序: 4.41; Android: √; iOS: √; HarmonyOS: 4.81; HarmonyOS(Vapor): x | 启动时的 query 参数 |
| apiCategory | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 需要基础库: 2.20.0 |
API 类别
可选值:
{}。(参见后文注意)
|
| scene | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | |
| chatType | number | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 从微信群聊/单聊打开小程序时,chatType 表示具体微信群聊/单聊类型可选值:
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| default | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| nativeFunctionalized | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| browseOnly | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| embedded | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| chatTool | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| appId | string | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 来源小程序、公众号或 App 的 appId |
| extraData | any | 否 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 来源小程序传过来的数据,scene=1037或1038时支持 |
| 合法值 | 兼容性 | 描述 |
|---|---|---|
| 1 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 2 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 3 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
| 4 | Web: -; 微信小程序: -; Android: -; iOS: -; HarmonyOS: - | - |
示例为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
示例
<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>
:::
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
|---|---|---|---|---|---|
| errMsg | string | 是 | - | Web: -; 微信小程序: 4.41; Android: -; iOS: -; HarmonyOS: - | 错误信息 |