1e41f4b71Sopenharmony_ci# @ohos.arkui.observer (无感监听) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci提供UI组件行为变化的无感监听能力。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 从API Version 11开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 导入模块 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## NavDestinationState 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciNavDestination组件状态。 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 25e41f4b71Sopenharmony_ci| --------- | --- | ------------------------ | 26e41f4b71Sopenharmony_ci| ON_SHOWN | 0 | NavDestination组件显示。<br/>**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 | 27e41f4b71Sopenharmony_ci| ON_HIDDEN | 1 | NavDestination组件隐藏。<br/>**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 | 28e41f4b71Sopenharmony_ci| ON_APPEAR<sup>12+</sup> | 2 | NavDestination从组件树上挂载。<br/> **卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。| 29e41f4b71Sopenharmony_ci| ON_DISAPPEAR<sup>12+</sup> | 3 | NavDestination从组件树上卸载。 <br/>**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。| 30e41f4b71Sopenharmony_ci| ON_WILL_SHOW<sup>12+</sup> | 4 | NavDestination组件显示之前。 <br/>**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。| 31e41f4b71Sopenharmony_ci| ON_WILL_HIDE<sup>12+</sup> | 5 | NavDestination组件隐藏之前。<br/>**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 | 32e41f4b71Sopenharmony_ci| ON_WILL_APPEAR<sup>12+</sup>| 6 | NavDestination挂载到组件树之前。<br/>**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。| 33e41f4b71Sopenharmony_ci| ON_WILL_DISAPPEAR<sup>12+</sup>| 7 | NavDestination从组件树上卸载之前。<br/>**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。| 34e41f4b71Sopenharmony_ci| ON_BACKPRESS<sup>12+</sup> | 100 | NavDestination组件返回。 <br/>**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。| 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci## ScrollEventType<sup>12+</sup> 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci滚动事件的类型。 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 45e41f4b71Sopenharmony_ci| --------- | --- | ------------------------ | 46e41f4b71Sopenharmony_ci| SCROLL_START | 0 | 滚动事件开始。 | 47e41f4b71Sopenharmony_ci| SCROLL_STOP | 1 | 滚动事件结束。 | 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci## RouterPageState 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_cirouterPage生命周期触发时对应的状态。 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 58e41f4b71Sopenharmony_ci| ----------------- | --- | ----------------------- | 59e41f4b71Sopenharmony_ci| ABOUT_TO_APPEAR | 0 | page即将显示。 | 60e41f4b71Sopenharmony_ci| ABOUT_TO_DISAPPEAR | 1 | page即将销毁。 | 61e41f4b71Sopenharmony_ci| ON_PAGE_SHOW | 2 | page显示。 | 62e41f4b71Sopenharmony_ci| ON_PAGE_HIDE | 3 | page隐藏。 | 63e41f4b71Sopenharmony_ci| ON_BACK_PRESS | 4 | page返回时。 | 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci## TabContentState<sup>12+</sup> 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ciTabContent组件的状态。 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci| 名称 | 值 | 说明 | 74e41f4b71Sopenharmony_ci| ----------------- | --- | ----------------------- | 75e41f4b71Sopenharmony_ci| ON_SHOW | 0 | TabContent组件显示。 | 76e41f4b71Sopenharmony_ci| ON_HIDE | 1 | TabContent组件隐藏。 | 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci## NavDestinationInfo 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ciNavDestination组件信息。 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 87e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------------- | ---- | -------------------------------------------- | 88e41f4b71Sopenharmony_ci| navigationId | [ResourceStr](arkui-ts/ts-types.md#resourcestr) | 是 | 包含NavDestination组件的Navigation组件的id。 | 89e41f4b71Sopenharmony_ci| name | [ResourceStr](arkui-ts/ts-types.md#resourcestr) | 是 | NavDestination组件的名称。 | 90e41f4b71Sopenharmony_ci| state | [NavDestinationState](#navdestinationstate) | 是 | NavDestination组件的状态。 | 91e41f4b71Sopenharmony_ci| index<sup>12+</sup> | number | 是 | NavDestination在页面栈中的索引。 | 92e41f4b71Sopenharmony_ci| param<sup>12+</sup> | Object | 否 | NavDestination组件的参数。 | 93e41f4b71Sopenharmony_ci| navDestinationId<sup>12+</sup> | string | 是 | NavDestination组件的唯一标识ID。 | 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci## NavigationInfo<sup>12+</sup> 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ciNavigation组件信息。 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 104e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------------- | ---- | -------------------------------------------- | 105e41f4b71Sopenharmony_ci| navigationId | string | 是 | Navigation组件的id。 | 106e41f4b71Sopenharmony_ci| pathStack | [NavPathStack](arkui-ts/ts-basic-components-navigation.md#navpathstack10) | 是 | Navigation组件的路由栈。 | 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci## ScrollEventInfo<sup>12+</sup> 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ciScrollEvent滚动信息。 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 117e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------------- | ---- | -------------------------------------------- | 118e41f4b71Sopenharmony_ci| id | string | 是 | 滚动组件的id。 | 119e41f4b71Sopenharmony_ci| uniqueId | number | 是 | 滚动组件的uniqueId。 | 120e41f4b71Sopenharmony_ci| scrollEvent | [ScrollEventType](#scrolleventtype12) | 是 | 滚动事件的类型。 | 121e41f4b71Sopenharmony_ci| offset | number | 是 | 滚动组件的当前偏移量。 | 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci## ObserverOptions<sup>12+</sup> 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ciObserver选项。 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 132e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------------- | ---- | -------------------------------------------- | 133e41f4b71Sopenharmony_ci| id | string | 是 | 组件的id。 | 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci## RouterPageInfo 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ciRouterPageInfo包含的信息。 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 144e41f4b71Sopenharmony_ci| -------------------- | ------------------------------------------------------------ | ---- | ---------------------------------------------- | 145e41f4b71Sopenharmony_ci| context | [UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) \| [UIContext](./js-apis-arkui-UIContext.md) | 是 | 触发生命周期的routerPage页面对应的上下文信息。 | 146e41f4b71Sopenharmony_ci| index | number | 是 | 触发生命周期的routerPage在栈中的位置。 | 147e41f4b71Sopenharmony_ci| name | string | 是 | 触发生命周期的routerPage页面的名称。 | 148e41f4b71Sopenharmony_ci| path | string | 是 | 触发生命周期的routerPage页面的路径。 | 149e41f4b71Sopenharmony_ci| state | [RouterPageState](#routerpagestate) | 是 | 触发生命周期的routerPage页面的状态。 | 150e41f4b71Sopenharmony_ci| pageId<sup>12+</sup> | string | 是 | 触发生命周期的routerPage页面的唯一标识。 | 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci## DensityInfo<sup>12+</sup> 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci屏幕像素密度变化回调包含的信息。 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 161e41f4b71Sopenharmony_ci| ------- | ----------------------------------------- | ---- | -------------------------------------- | 162e41f4b71Sopenharmony_ci| context | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 屏幕像素密度变化时页面对应的上下文信息。 | 163e41f4b71Sopenharmony_ci| density | number | 是 | 变化后的屏幕像素密度。 | 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci## NavDestinationSwitchInfo<sup>12+</sup> 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ciNavigation组件页面切换的信息。 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 174e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------------- | ---- | -------------------------------------------- | 175e41f4b71Sopenharmony_ci| context | [UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) \| [UIContext](./js-apis-arkui-UIContext.md) | 是 | 触发页面切换的Navigation对应的上下文信息。 | 176e41f4b71Sopenharmony_ci| from | [NavDestinationInfo](#navdestinationinfo) \| [NavBar](./arkui-ts/ts-basic-components-navigation.md#navbar12) | 是 | 页面切换的源页面。 | 177e41f4b71Sopenharmony_ci| to | [NavDestinationInfo](#navdestinationinfo) \| [NavBar](./arkui-ts/ts-basic-components-navigation.md#navbar12) | 是 | 页面切换的目的页面。 | 178e41f4b71Sopenharmony_ci| operation | [NavigationOperation](./arkui-ts/ts-basic-components-navigation.md#navigationoperation11枚举说明) | 是 | 页面切换操作类型。 | 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci## NavDestinationSwitchObserverOptions<sup>12+</sup> 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ciNavigation组件页面切换事件的监听选项。 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 189e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------------- | ---- | -------------------------------------------- | 190e41f4b71Sopenharmony_ci| navigationId | [ResourceStr](arkui-ts/ts-types.md#resourcestr) | 是 | 指定需要监听的Navigation的ID | 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci## TabContentInfo<sup>12+</sup> 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ciTabContent页面的切换信息。 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 201e41f4b71Sopenharmony_ci| ------------ | -------------------------------------------------- | ---- | -------------------------------------------- | 202e41f4b71Sopenharmony_ci| tabContentId | string | 是 | TabContent组件的id。 | 203e41f4b71Sopenharmony_ci| tabContentUniqueId | number | 是 | TabContent组件的uniqueId。 | 204e41f4b71Sopenharmony_ci| state | [TabContentState](#tabcontentstate12) | 是 | TabContent组件的状态。 | 205e41f4b71Sopenharmony_ci| id | string | 是 | Tabs组件的id。 | 206e41f4b71Sopenharmony_ci| uniqueId | number | 是 | Tabs组件的uniqueId。 | 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci## observer.on('navDestinationUpdate') 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_cion(type: 'navDestinationUpdate', callback: Callback\<NavDestinationInfo\>): void 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci监听NavDestination组件的状态变化。 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci**参数:** 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 221e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------------------ | 222e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'navDestinationUpdate',即NavDestination组件的状态变化。 | 223e41f4b71Sopenharmony_ci| callback | Callback\<[NavDestinationInfo](#navdestinationinfo)\> | 是 | 回调函数。返回当前的NavDestination组件状态。 | 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci**示例:** 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci```ts 228e41f4b71Sopenharmony_ciobserver.on('navDestinationUpdate', (info) => { 229e41f4b71Sopenharmony_ci console.info('NavDestination state update', JSON.stringify(info)); 230e41f4b71Sopenharmony_ci}); 231e41f4b71Sopenharmony_ci``` 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci## observer.off('navDestinationUpdate') 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_cioff(type: 'navDestinationUpdate', callback?: Callback\<NavDestinationInfo\>): void 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci取消监听NavDestination组件的状态变化。 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci**参数:** 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 246e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------------------ | 247e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'navDestinationUpdate',即NavDestination组件的状态变化。 | 248e41f4b71Sopenharmony_ci| callback | Callback\<[NavDestinationInfo](#navdestinationinfo)\> | 否 | 回调函数。返回当前的NavDestination组件状态。 | 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci**示例:** 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci```ts 253e41f4b71Sopenharmony_ciobserver.off('navDestinationUpdate'); 254e41f4b71Sopenharmony_ci``` 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci## observer.on('navDestinationUpdate') 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_cion(type: 'navDestinationUpdate', options: { navigationId: ResourceStr }, callback: Callback\<NavDestinationInfo\>): void 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci监听NavDestination组件的状态变化。 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci**参数:** 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 269e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------------------------- | ---- | ------------------------------------------------------------------------ | 270e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'navDestinationUpdate',即NavDestination组件的状态变化。 | 271e41f4b71Sopenharmony_ci| options | { navigationId: [ResourceStr](arkui-ts/ts-types.md#resourcestr) } | 是 | 指定监听的Navigation的id。 | 272e41f4b71Sopenharmony_ci| callback | Callback\<[NavDestinationInfo](#navdestinationinfo)\> | 是 | 回调函数。返回当前的NavDestination组件状态。 | 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci**示例:** 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci```ts 277e41f4b71Sopenharmony_ciobserver.on('navDestinationUpdate', { navigationId: "testId" }, (info) => { 278e41f4b71Sopenharmony_ci console.info('NavDestination state update', JSON.stringify(info)); 279e41f4b71Sopenharmony_ci}); 280e41f4b71Sopenharmony_ci``` 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci## observer.off('navDestinationUpdate') 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_cioff(type: 'navDestinationUpdate', options: { navigationId: ResourceStr }, callback?: Callback\<NavDestinationInfo\>): void 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci取消监听NavDestination组件的状态变化。 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci**参数:** 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 295e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------------------------- | ---- | ------------------------------------------------------------------------ | 296e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'navDestinationUpdate',即NavDestination组件的状态变化。 | 297e41f4b71Sopenharmony_ci| options | { navigationId: [ResourceStr](arkui-ts/ts-types.md#resourcestr) } | 是 | 指定监听的Navigation的id。 | 298e41f4b71Sopenharmony_ci| callback | Callback\<[NavDestinationInfo](#navdestinationinfo)\> | 否 | 回调函数。返回当前的NavDestination组件状态。 | 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci**示例:** 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci```ts 303e41f4b71Sopenharmony_ciobserver.off('navDestinationUpdate', { navigationId: "testId" }); 304e41f4b71Sopenharmony_ci``` 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci## observer.on('scrollEvent')<sup>12+</sup> 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_cion(type: 'scrollEvent', callback: Callback\<ScrollEventInfo\>): void 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci监听滚动事件的开始和结束。 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci**参数:** 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 319e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------------------ | 320e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'scrollEvent',即滚动事件的开始和结束。 | 321e41f4b71Sopenharmony_ci| callback | Callback\<[ScrollEventInfo](#scrolleventinfo12)\> | 是 | 回调函数。返回滚动事件的信息。 | 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci**示例:** 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci参考[offscrollevent](#observeroffscrollevent12-1)示例。 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci## observer.off('scrollEvent')<sup>12+</sup> 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_cioff(type: 'scrollEvent', callback?: Callback\<ScrollEventInfo\>): void 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci取消监听滚动事件的开始和结束。 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci**参数:** 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 340e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | ---- | ------------------------------------------------------------------------ | 341e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'scrollEvent',即滚动事件的开始和结束。 | 342e41f4b71Sopenharmony_ci| callback | Callback\<[ScrollEventInfo](#scrolleventinfo12)\> | 否 | 回调函数。返回滚动事件的信息。 | 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci**示例:** 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci参考[offscrollevent](#observeroffscrollevent12-1)示例。 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ci## observer.on('scrollEvent')<sup>12+</sup> 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_cion(type: 'scrollEvent', options: ObserverOptions, callback: Callback\<ScrollEventInfo\>): void 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ci监听滚动事件的开始和结束。 353e41f4b71Sopenharmony_ci 354e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 357e41f4b71Sopenharmony_ci 358e41f4b71Sopenharmony_ci**参数:** 359e41f4b71Sopenharmony_ci 360e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 361e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------------------------- | ---- | ------------------------------------------------------------------------ | 362e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'scrollEvent',即滚动事件的开始和结束。 | 363e41f4b71Sopenharmony_ci| options | [ObserverOptions](#observeroptions12) | 是 | 指定监听的滚动组件的id。 | 364e41f4b71Sopenharmony_ci| callback | Callback\<[ScrollEventInfo](#scrolleventinfo12)\> | 是 | 回调函数。返回滚动事件的信息。 | 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci**示例:** 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci参考[offscrollevent](#observeroffscrollevent12-1)示例。 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci## observer.off('scrollEvent')<sup>12+</sup> 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_cioff(type: 'scrollEvent', options: ObserverOptions, callback?: Callback\<ScrollEventInfo\>): void 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci取消监听滚动事件的开始和结束。 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 379e41f4b71Sopenharmony_ci 380e41f4b71Sopenharmony_ci**参数:** 381e41f4b71Sopenharmony_ci 382e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 383e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------------------------- | ---- | ------------------------------------------------------------------------ | 384e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'scrollEvent',即滚动事件的开始和结束。 | 385e41f4b71Sopenharmony_ci| options | [ObserverOptions](#observeroptions12) | 是 | 指定监听的滚动组件的id。 | 386e41f4b71Sopenharmony_ci| callback | Callback\<[ScrollEventInfo](#scrolleventinfo12)\> | 否 | 回调函数。返回滚动事件的信息。 | 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci**示例:** 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ci```ts 391e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI' 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci@Entry 394e41f4b71Sopenharmony_ci@Component 395e41f4b71Sopenharmony_cistruct Index { 396e41f4b71Sopenharmony_ci scroller: Scroller = new Scroller(); 397e41f4b71Sopenharmony_ci options: observer.ObserverOptions = { id: "testId" }; 398e41f4b71Sopenharmony_ci private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7] 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci build() { 401e41f4b71Sopenharmony_ci Column() { 402e41f4b71Sopenharmony_ci Column() { 403e41f4b71Sopenharmony_ci Scroll(this.scroller) { 404e41f4b71Sopenharmony_ci Column() { 405e41f4b71Sopenharmony_ci ForEach(this.arr, (item: number) => { 406e41f4b71Sopenharmony_ci Text(item.toString()) 407e41f4b71Sopenharmony_ci .width('90%') 408e41f4b71Sopenharmony_ci .height(150) 409e41f4b71Sopenharmony_ci .backgroundColor(0xFFFFFF) 410e41f4b71Sopenharmony_ci .borderRadius(15) 411e41f4b71Sopenharmony_ci .fontSize(16) 412e41f4b71Sopenharmony_ci .textAlign(TextAlign.Center) 413e41f4b71Sopenharmony_ci .margin({ top: 10 }) 414e41f4b71Sopenharmony_ci }, (item: string) => item) 415e41f4b71Sopenharmony_ci }.width('100%') 416e41f4b71Sopenharmony_ci } 417e41f4b71Sopenharmony_ci .id("testId") 418e41f4b71Sopenharmony_ci .height('80%') 419e41f4b71Sopenharmony_ci } 420e41f4b71Sopenharmony_ci .width('100%') 421e41f4b71Sopenharmony_ci 422e41f4b71Sopenharmony_ci Row() { 423e41f4b71Sopenharmony_ci Button('UIObserver on') 424e41f4b71Sopenharmony_ci .onClick(() => { 425e41f4b71Sopenharmony_ci observer.on('scrollEvent', (info) => { 426e41f4b71Sopenharmony_ci console.info('scrollEventInfo', JSON.stringify(info)); 427e41f4b71Sopenharmony_ci }); 428e41f4b71Sopenharmony_ci }) 429e41f4b71Sopenharmony_ci Button('UIObserver off') 430e41f4b71Sopenharmony_ci .onClick(() => { 431e41f4b71Sopenharmony_ci observer.off('scrollEvent'); 432e41f4b71Sopenharmony_ci }) 433e41f4b71Sopenharmony_ci } 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ci Row() { 436e41f4b71Sopenharmony_ci Button('UIObserverWithId on') 437e41f4b71Sopenharmony_ci .onClick(() => { 438e41f4b71Sopenharmony_ci observer.on('scrollEvent', this.options, (info) => { 439e41f4b71Sopenharmony_ci console.info('scrollEventInfo', JSON.stringify(info)); 440e41f4b71Sopenharmony_ci }); 441e41f4b71Sopenharmony_ci }) 442e41f4b71Sopenharmony_ci Button('UIObserverWithId off') 443e41f4b71Sopenharmony_ci .onClick(() => { 444e41f4b71Sopenharmony_ci observer.off('scrollEvent', this.options); 445e41f4b71Sopenharmony_ci }) 446e41f4b71Sopenharmony_ci } 447e41f4b71Sopenharmony_ci } 448e41f4b71Sopenharmony_ci .height('100%') 449e41f4b71Sopenharmony_ci } 450e41f4b71Sopenharmony_ci} 451e41f4b71Sopenharmony_ci``` 452e41f4b71Sopenharmony_ci 453e41f4b71Sopenharmony_ci## observer.on('routerPageUpdate')<sup>11+</sup> 454e41f4b71Sopenharmony_ci 455e41f4b71Sopenharmony_cion(type: 'routerPageUpdate', context: UIAbilityContext | UIContext, callback: Callback\<RouterPageInfo\>): void 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci监听router中page页面的状态变化。 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ci**参数:** 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 466e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 467e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'routerPageUpdate',即router中page页面的状态变化。 | 468e41f4b71Sopenharmony_ci| context | [UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) \| [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面的范围。 | 469e41f4b71Sopenharmony_ci| callback | Callback\<[RouterPageInfo](#routerpageinfo)\> | 是 | 回调函数。携带pageInfo,返回当前的page页面状态。 | 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci**示例:** 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci```ts 474e41f4b71Sopenharmony_ci// used in UIAbility 475e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 476e41f4b71Sopenharmony_ciimport { UIContext, window, uiObserver as observer } from '@kit.ArkUI'; 477e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 480e41f4b71Sopenharmony_ci private uiContext: UIContext | null = null; 481e41f4b71Sopenharmony_ci 482e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 483e41f4b71Sopenharmony_ci // 注册监听,范围是abilityContext内的page 484e41f4b71Sopenharmony_ci observer.on('routerPageUpdate', this.context, (info: observer.RouterPageInfo) => { 485e41f4b71Sopenharmony_ci console.info('[uiObserver][abilityContext] got info: ' + JSON.stringify(info)) 486e41f4b71Sopenharmony_ci }) 487e41f4b71Sopenharmony_ci } 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage): void { 490e41f4b71Sopenharmony_ci windowStage.loadContent('pages/Index', (err) => { 491e41f4b71Sopenharmony_ci windowStage.getMainWindow((err: BusinessError, data) => { 492e41f4b71Sopenharmony_ci let windowInfo: window.Window = data; 493e41f4b71Sopenharmony_ci // 获取UIContext实例 494e41f4b71Sopenharmony_ci this.uiContext = windowInfo.getUIContext(); 495e41f4b71Sopenharmony_ci // 注册监听,范围是uiContext内的page 496e41f4b71Sopenharmony_ci observer.on('routerPageUpdate', this.uiContext, (info: observer.RouterPageInfo)=>{ 497e41f4b71Sopenharmony_ci console.info('[uiObserver][uiContext] got info: ' + JSON.stringify(info)) 498e41f4b71Sopenharmony_ci }) 499e41f4b71Sopenharmony_ci }) 500e41f4b71Sopenharmony_ci }); 501e41f4b71Sopenharmony_ci } 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci // ... other function in EntryAbility 504e41f4b71Sopenharmony_ci} 505e41f4b71Sopenharmony_ci``` 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ci## observer.off('routerPageUpdate')<sup>11+</sup> 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_cioff(type: 'routerPageUpdate', context: UIAbilityContext | UIContext, callback?: Callback\<RouterPageInfo\>): void 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_ci取消监听router中page页面的状态变化。 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci**参数:** 518e41f4b71Sopenharmony_ci 519e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 520e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 521e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'routerPageUpdate',即router中page页面的状态变化。 | 522e41f4b71Sopenharmony_ci| context | [UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) \| [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面的范围 | 523e41f4b71Sopenharmony_ci| callback | Callback\<[RouterPageInfo](#routerpageinfo)\> | 否 | 需要被注销的回调函。 | 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ci**示例:** 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci```ts 528e41f4b71Sopenharmony_ci// used in UIAbility 529e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 530e41f4b71Sopenharmony_ciimport { uiObserver as observer, UIContext } from '@kit.ArkUI'; 531e41f4b71Sopenharmony_ci 532e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 533e41f4b71Sopenharmony_ci // 实际使用前uiContext需要被赋值。参见示例observer.on('routerPageUpdate') 534e41f4b71Sopenharmony_ci private uiContext: UIContext | null = null; 535e41f4b71Sopenharmony_ci 536e41f4b71Sopenharmony_ci onDestroy(): void { 537e41f4b71Sopenharmony_ci // 注销当前abilityContext上的所有routerPageUpdate监听 538e41f4b71Sopenharmony_ci observer.off('routerPageUpdate', this.context) 539e41f4b71Sopenharmony_ci } 540e41f4b71Sopenharmony_ci 541e41f4b71Sopenharmony_ci onWindowStageDestroy(): void { 542e41f4b71Sopenharmony_ci // 注销在uiContext上的所有routerPageUpdate监听 543e41f4b71Sopenharmony_ci if (this.uiContext) { 544e41f4b71Sopenharmony_ci observer.off('routerPageUpdate', this.uiContext); 545e41f4b71Sopenharmony_ci } 546e41f4b71Sopenharmony_ci } 547e41f4b71Sopenharmony_ci 548e41f4b71Sopenharmony_ci // ... other function in EntryAbility 549e41f4b71Sopenharmony_ci} 550e41f4b71Sopenharmony_ci``` 551e41f4b71Sopenharmony_ci 552e41f4b71Sopenharmony_ci## observer.on('densityUpdate')<sup>12+</sup> 553e41f4b71Sopenharmony_ci 554e41f4b71Sopenharmony_cion(type: 'densityUpdate', context: UIContext, callback: Callback\<DensityInfo\>): void 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci监听屏幕像素密度变化。 557e41f4b71Sopenharmony_ci 558e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci**参数:** 563e41f4b71Sopenharmony_ci 564e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 565e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 566e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'densityUpdate',即屏幕像素密度变化。 | 567e41f4b71Sopenharmony_ci| context | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面的范围。 | 568e41f4b71Sopenharmony_ci| callback | Callback\<[DensityInfo](#densityinfo12)\> | 是 | 回调函数。携带densityInfo,返回变化后的屏幕像素密度。 | 569e41f4b71Sopenharmony_ci 570e41f4b71Sopenharmony_ci**示例:** 571e41f4b71Sopenharmony_ci 572e41f4b71Sopenharmony_ci```ts 573e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 574e41f4b71Sopenharmony_ci 575e41f4b71Sopenharmony_ci@Entry 576e41f4b71Sopenharmony_ci@Component 577e41f4b71Sopenharmony_cistruct Index { 578e41f4b71Sopenharmony_ci @State density: number = 0; 579e41f4b71Sopenharmony_ci @State message: string = '未注册监听' 580e41f4b71Sopenharmony_ci 581e41f4b71Sopenharmony_ci densityUpdateCallback = (info: observer.DensityInfo) => { 582e41f4b71Sopenharmony_ci this.density = info.density; 583e41f4b71Sopenharmony_ci this.message = '变化后的DPI:' + this.density.toString(); 584e41f4b71Sopenharmony_ci } 585e41f4b71Sopenharmony_ci 586e41f4b71Sopenharmony_ci build() { 587e41f4b71Sopenharmony_ci Column() { 588e41f4b71Sopenharmony_ci Text(this.message) 589e41f4b71Sopenharmony_ci .fontSize(24) 590e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 591e41f4b71Sopenharmony_ci Button('注册屏幕像素密度变化监听') 592e41f4b71Sopenharmony_ci .onClick(() => { 593e41f4b71Sopenharmony_ci this.message = '已注册监听' 594e41f4b71Sopenharmony_ci observer.on('densityUpdate', this.getUIContext(), this.densityUpdateCallback); 595e41f4b71Sopenharmony_ci }) 596e41f4b71Sopenharmony_ci } 597e41f4b71Sopenharmony_ci } 598e41f4b71Sopenharmony_ci} 599e41f4b71Sopenharmony_ci``` 600e41f4b71Sopenharmony_ci 601e41f4b71Sopenharmony_ci## observer.off('densityUpdate')<sup>12+</sup> 602e41f4b71Sopenharmony_ci 603e41f4b71Sopenharmony_cioff(type: 'densityUpdate', context: UIContext, callback?: Callback\<DensityInfo\>): void 604e41f4b71Sopenharmony_ci 605e41f4b71Sopenharmony_ci取消监听屏幕像素密度的变化。 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 608e41f4b71Sopenharmony_ci 609e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 610e41f4b71Sopenharmony_ci 611e41f4b71Sopenharmony_ci**参数:** 612e41f4b71Sopenharmony_ci 613e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 614e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ---------------------------------------------------------------------------------------------- | 615e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'densityUpdate',即屏幕像素密度变化。 | 616e41f4b71Sopenharmony_ci| context | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面的范围。 | 617e41f4b71Sopenharmony_ci| callback | Callback\<[DensityInfo](#densityinfo12)\> | 否 | 需要被注销的回调函数。若不指定具体的回调函数,则注销指定UIContext下所有densityUpdate事件监听。 | 618e41f4b71Sopenharmony_ci 619e41f4b71Sopenharmony_ci```ts 620e41f4b71Sopenharmony_ciimport { uiObserver as observer, UIContext } from '@kit.ArkUI'; 621e41f4b71Sopenharmony_ci 622e41f4b71Sopenharmony_ci@Entry 623e41f4b71Sopenharmony_ci@Component 624e41f4b71Sopenharmony_cistruct Index { 625e41f4b71Sopenharmony_ci @State density: number = 0; 626e41f4b71Sopenharmony_ci @State message: string = '未注册监听' 627e41f4b71Sopenharmony_ci 628e41f4b71Sopenharmony_ci densityUpdateCallback = (info: observer.DensityInfo) => { 629e41f4b71Sopenharmony_ci this.density = info.density; 630e41f4b71Sopenharmony_ci this.message = '变化后的DPI:' + this.density.toString(); 631e41f4b71Sopenharmony_ci } 632e41f4b71Sopenharmony_ci 633e41f4b71Sopenharmony_ci build() { 634e41f4b71Sopenharmony_ci Column() { 635e41f4b71Sopenharmony_ci Text(this.message) 636e41f4b71Sopenharmony_ci .fontSize(24) 637e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 638e41f4b71Sopenharmony_ci Button('注册屏幕像素密度变化监听') 639e41f4b71Sopenharmony_ci .onClick(() => { 640e41f4b71Sopenharmony_ci this.message = '已注册监听' 641e41f4b71Sopenharmony_ci observer.on('densityUpdate', this.getUIContext(), this.densityUpdateCallback); 642e41f4b71Sopenharmony_ci }) 643e41f4b71Sopenharmony_ci Button('解除注册屏幕像素密度变化监听') 644e41f4b71Sopenharmony_ci .onClick(() => { 645e41f4b71Sopenharmony_ci this.message = '未注册监听' 646e41f4b71Sopenharmony_ci observer.off('densityUpdate', this.getUIContext(), this.densityUpdateCallback); 647e41f4b71Sopenharmony_ci }) 648e41f4b71Sopenharmony_ci } 649e41f4b71Sopenharmony_ci } 650e41f4b71Sopenharmony_ci} 651e41f4b71Sopenharmony_ci``` 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci## observer.on('willDraw')<sup>12+</sup> 654e41f4b71Sopenharmony_ci 655e41f4b71Sopenharmony_cion(type: 'willDraw', context: UIContext, callback: Callback\<void\>): void 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ci监听每一帧绘制指令下发情况。 658e41f4b71Sopenharmony_ci 659e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ci**参数:** 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 666e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 667e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'willDraw',即是否将要绘制。 | 668e41f4b71Sopenharmony_ci| context | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面的范围。 | 669e41f4b71Sopenharmony_ci| callback | Callback\<void\> | 是 | 回调函数。 | 670e41f4b71Sopenharmony_ci 671e41f4b71Sopenharmony_ci**示例:** 672e41f4b71Sopenharmony_ci 673e41f4b71Sopenharmony_ci```ts 674e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 675e41f4b71Sopenharmony_ci 676e41f4b71Sopenharmony_ci@Entry 677e41f4b71Sopenharmony_ci@Component 678e41f4b71Sopenharmony_cistruct Index { 679e41f4b71Sopenharmony_ci willDrawCallback = () => { 680e41f4b71Sopenharmony_ci console.info("willDraw指令下发"); 681e41f4b71Sopenharmony_ci } 682e41f4b71Sopenharmony_ci build() { 683e41f4b71Sopenharmony_ci Column() { 684e41f4b71Sopenharmony_ci Button('注册绘制指令下发监听') 685e41f4b71Sopenharmony_ci .onClick(() => { 686e41f4b71Sopenharmony_ci observer.on('willDraw', this.getUIContext(), this.willDrawCallback); 687e41f4b71Sopenharmony_ci }) 688e41f4b71Sopenharmony_ci } 689e41f4b71Sopenharmony_ci } 690e41f4b71Sopenharmony_ci} 691e41f4b71Sopenharmony_ci``` 692e41f4b71Sopenharmony_ci 693e41f4b71Sopenharmony_ci## observer.off('willDraw')<sup>12+</sup> 694e41f4b71Sopenharmony_ci 695e41f4b71Sopenharmony_cioff(type: 'willDraw', context: UIContext, callback?: Callback\<void\>): void 696e41f4b71Sopenharmony_ci 697e41f4b71Sopenharmony_ci取消监听每一帧绘制指令下发情况。 698e41f4b71Sopenharmony_ci 699e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 700e41f4b71Sopenharmony_ci 701e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 702e41f4b71Sopenharmony_ci 703e41f4b71Sopenharmony_ci**参数:** 704e41f4b71Sopenharmony_ci 705e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 706e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ----------------------------------------------------- | 707e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'willDraw',即是否将要绘制。 | 708e41f4b71Sopenharmony_ci| context | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面的范围。 | 709e41f4b71Sopenharmony_ci| callback | Callback\<void\> | 否 | 需要被注销的回调函数。 | 710e41f4b71Sopenharmony_ci 711e41f4b71Sopenharmony_ci```ts 712e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 713e41f4b71Sopenharmony_ci 714e41f4b71Sopenharmony_ci@Entry 715e41f4b71Sopenharmony_ci@Component 716e41f4b71Sopenharmony_cistruct Index { 717e41f4b71Sopenharmony_ci willDrawCallback = () => { 718e41f4b71Sopenharmony_ci console.info("willDraw指令下发") 719e41f4b71Sopenharmony_ci } 720e41f4b71Sopenharmony_ci 721e41f4b71Sopenharmony_ci build() { 722e41f4b71Sopenharmony_ci Column() { 723e41f4b71Sopenharmony_ci Button('注册绘制指令下发监听') 724e41f4b71Sopenharmony_ci .onClick(() => { 725e41f4b71Sopenharmony_ci observer.on('willDraw', this.getUIContext(), this.willDrawCallback); 726e41f4b71Sopenharmony_ci }) 727e41f4b71Sopenharmony_ci Button('解除注册绘制指令下发监听') 728e41f4b71Sopenharmony_ci .onClick(() => { 729e41f4b71Sopenharmony_ci observer.off('willDraw', this.getUIContext(), this.willDrawCallback); 730e41f4b71Sopenharmony_ci }) 731e41f4b71Sopenharmony_ci } 732e41f4b71Sopenharmony_ci } 733e41f4b71Sopenharmony_ci} 734e41f4b71Sopenharmony_ci``` 735e41f4b71Sopenharmony_ci 736e41f4b71Sopenharmony_ci## observer.on('didLayout')<sup>12+</sup> 737e41f4b71Sopenharmony_ci 738e41f4b71Sopenharmony_cion(type: 'didLayout', context: UIContext, callback: Callback\<void\>): void 739e41f4b71Sopenharmony_ci 740e41f4b71Sopenharmony_ci监听每一帧布局完成情况 741e41f4b71Sopenharmony_ci 742e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 743e41f4b71Sopenharmony_ci 744e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 745e41f4b71Sopenharmony_ci 746e41f4b71Sopenharmony_ci**参数:** 747e41f4b71Sopenharmony_ci 748e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 749e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 750e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'didLayout',即是否布局完成。 | 751e41f4b71Sopenharmony_ci| context | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面的范围。 | 752e41f4b71Sopenharmony_ci| callback | Callback\<void\> | 是 | 回调函数。 | 753e41f4b71Sopenharmony_ci 754e41f4b71Sopenharmony_ci**示例:** 755e41f4b71Sopenharmony_ci 756e41f4b71Sopenharmony_ci```ts 757e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 758e41f4b71Sopenharmony_ci 759e41f4b71Sopenharmony_ci@Entry 760e41f4b71Sopenharmony_ci@Component 761e41f4b71Sopenharmony_cistruct Index { 762e41f4b71Sopenharmony_ci didLayoutCallback = () => { 763e41f4b71Sopenharmony_ci console.info("Layout布局完成"); 764e41f4b71Sopenharmony_ci } 765e41f4b71Sopenharmony_ci build() { 766e41f4b71Sopenharmony_ci Column() { 767e41f4b71Sopenharmony_ci Button('注册布局完成监听') 768e41f4b71Sopenharmony_ci .onClick(() => { 769e41f4b71Sopenharmony_ci observer.on('didLayout', this.getUIContext(), this.didLayoutCallback); 770e41f4b71Sopenharmony_ci }) 771e41f4b71Sopenharmony_ci } 772e41f4b71Sopenharmony_ci } 773e41f4b71Sopenharmony_ci} 774e41f4b71Sopenharmony_ci``` 775e41f4b71Sopenharmony_ci 776e41f4b71Sopenharmony_ci## observer.off('didLayout')<sup>12+</sup> 777e41f4b71Sopenharmony_ci 778e41f4b71Sopenharmony_cioff(type: 'didLayout', context: UIContext, callback?: Callback\<void\>): void 779e41f4b71Sopenharmony_ci 780e41f4b71Sopenharmony_ci取消监听每一帧布局完成情况。 781e41f4b71Sopenharmony_ci 782e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 783e41f4b71Sopenharmony_ci 784e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 785e41f4b71Sopenharmony_ci 786e41f4b71Sopenharmony_ci**参数:** 787e41f4b71Sopenharmony_ci 788e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 789e41f4b71Sopenharmony_ci| -------- | ----------------------------------------- | ---- | ----------------------------------------------------- | 790e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'didLayout',即是否布局完成。 | 791e41f4b71Sopenharmony_ci| context | [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面的范围。 | 792e41f4b71Sopenharmony_ci| callback | Callback\<void\> | 否 | 需要被注销的回调函数。 | 793e41f4b71Sopenharmony_ci 794e41f4b71Sopenharmony_ci```ts 795e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 796e41f4b71Sopenharmony_ci 797e41f4b71Sopenharmony_ci@Entry 798e41f4b71Sopenharmony_ci@Component 799e41f4b71Sopenharmony_cistruct Index { 800e41f4b71Sopenharmony_ci didLayoutCallback = () => { 801e41f4b71Sopenharmony_ci console.info("Layout布局完成") 802e41f4b71Sopenharmony_ci } 803e41f4b71Sopenharmony_ci 804e41f4b71Sopenharmony_ci build() { 805e41f4b71Sopenharmony_ci Column() { 806e41f4b71Sopenharmony_ci Button('注册布局完成监听') 807e41f4b71Sopenharmony_ci .onClick(() => { 808e41f4b71Sopenharmony_ci observer.on('didLayout', this.getUIContext(), this.didLayoutCallback); 809e41f4b71Sopenharmony_ci }) 810e41f4b71Sopenharmony_ci Button('解除布局完成s监听') 811e41f4b71Sopenharmony_ci .onClick(() => { 812e41f4b71Sopenharmony_ci observer.off('didLayout', this.getUIContext(), this.didLayoutCallback); 813e41f4b71Sopenharmony_ci }) 814e41f4b71Sopenharmony_ci } 815e41f4b71Sopenharmony_ci } 816e41f4b71Sopenharmony_ci} 817e41f4b71Sopenharmony_ci``` 818e41f4b71Sopenharmony_ci 819e41f4b71Sopenharmony_ci## observer.on('navDestinationSwitch')<sup>12+</sup> 820e41f4b71Sopenharmony_ci 821e41f4b71Sopenharmony_cion(type: 'navDestinationSwitch', context: UIAbilityContext | UIContext, callback: Callback\<NavDestinationSwitchInfo\>): void 822e41f4b71Sopenharmony_ci 823e41f4b71Sopenharmony_ci监听Navigation的页面切换事件。 824e41f4b71Sopenharmony_ci 825e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 826e41f4b71Sopenharmony_ci 827e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ci**参数:** 830e41f4b71Sopenharmony_ci 831e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 832e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 833e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'navDestinationSwitch',即Navigation的页面切换事件。 | 834e41f4b71Sopenharmony_ci| context | [UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) \| [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面切换事件的范围。 | 835e41f4b71Sopenharmony_ci| callback | Callback\<[NavDestinationSwitchInfo](#navdestinationswitchinfo12)\> | 是 | 回调函数。携带NavDestinationSwitchInfo,返回页面切换事件的信息。 | 836e41f4b71Sopenharmony_ci 837e41f4b71Sopenharmony_ci**示例:** 838e41f4b71Sopenharmony_ci 839e41f4b71Sopenharmony_ci```ts 840e41f4b71Sopenharmony_ci// EntryAbility.ets 841e41f4b71Sopenharmony_ci// 演示 observer.on('navDestinationSwitch', UIAbilityContext, callback) 842e41f4b71Sopenharmony_ci// observer.off('navDestinationSwitch', UIAbilityContext, callback) 843e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 844e41f4b71Sopenharmony_ciimport { uiObserver as observer, window } from '@kit.ArkUI'; 845e41f4b71Sopenharmony_ciimport { hilog } from "@kit.PerformanceAnalysisKit" 846e41f4b71Sopenharmony_ci 847e41f4b71Sopenharmony_cifunction callBackFunc(info: observer.NavDestinationSwitchInfo) { 848e41f4b71Sopenharmony_ci console.info(`testTag navDestinationSwitch from: ${JSON.stringify(info.from)} to: ${JSON.stringify(info.to)}`) 849e41f4b71Sopenharmony_ci} 850e41f4b71Sopenharmony_ci 851e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 852e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 853e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 854e41f4b71Sopenharmony_ci observer.on('navDestinationSwitch', this.context, callBackFunc); 855e41f4b71Sopenharmony_ci } 856e41f4b71Sopenharmony_ci 857e41f4b71Sopenharmony_ci onDestroy(): void { 858e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 859e41f4b71Sopenharmony_ci observer.off('navDestinationSwitch', this.context, callBackFunc); 860e41f4b71Sopenharmony_ci } 861e41f4b71Sopenharmony_ci 862e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage): void { 863e41f4b71Sopenharmony_ci // Main window is created, set main page for this ability 864e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 865e41f4b71Sopenharmony_ci 866e41f4b71Sopenharmony_ci windowStage.loadContent('pages/Index', (err, data) => { 867e41f4b71Sopenharmony_ci if (err.code) { 868e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 869e41f4b71Sopenharmony_ci return; 870e41f4b71Sopenharmony_ci } 871e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 872e41f4b71Sopenharmony_ci }); 873e41f4b71Sopenharmony_ci } 874e41f4b71Sopenharmony_ci 875e41f4b71Sopenharmony_ci onWindowStageDestroy(): void { 876e41f4b71Sopenharmony_ci // Main window is destroyed, release UI related resources 877e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 878e41f4b71Sopenharmony_ci } 879e41f4b71Sopenharmony_ci 880e41f4b71Sopenharmony_ci onForeground(): void { 881e41f4b71Sopenharmony_ci // Ability has brought to foreground 882e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 883e41f4b71Sopenharmony_ci } 884e41f4b71Sopenharmony_ci 885e41f4b71Sopenharmony_ci onBackground(): void { 886e41f4b71Sopenharmony_ci // Ability has back to background 887e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 888e41f4b71Sopenharmony_ci } 889e41f4b71Sopenharmony_ci} 890e41f4b71Sopenharmony_ci``` 891e41f4b71Sopenharmony_ci 892e41f4b71Sopenharmony_ci```ts 893e41f4b71Sopenharmony_ci// Index.ets 894e41f4b71Sopenharmony_ci// 演示 observer.on('navDestinationSwitch', UIContext, callback) 895e41f4b71Sopenharmony_ci// observer.off('navDestinationSwitch', UIContext, callback) 896e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 897e41f4b71Sopenharmony_ci 898e41f4b71Sopenharmony_ci@Component 899e41f4b71Sopenharmony_cistruct PageOne { 900e41f4b71Sopenharmony_ci build() { 901e41f4b71Sopenharmony_ci NavDestination() { 902e41f4b71Sopenharmony_ci Text("pageOne") 903e41f4b71Sopenharmony_ci }.title("pageOne") 904e41f4b71Sopenharmony_ci } 905e41f4b71Sopenharmony_ci} 906e41f4b71Sopenharmony_ci 907e41f4b71Sopenharmony_cifunction callBackFunc(info: observer.NavDestinationSwitchInfo) { 908e41f4b71Sopenharmony_ci console.info(`testTag navDestinationSwitch from: ${JSON.stringify(info.from)} to: ${JSON.stringify(info.to)}`) 909e41f4b71Sopenharmony_ci} 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_ci@Entry 912e41f4b71Sopenharmony_ci@Component 913e41f4b71Sopenharmony_cistruct Index { 914e41f4b71Sopenharmony_ci private stack: NavPathStack = new NavPathStack(); 915e41f4b71Sopenharmony_ci 916e41f4b71Sopenharmony_ci @Builder 917e41f4b71Sopenharmony_ci PageBuilder(name: string) { 918e41f4b71Sopenharmony_ci PageOne() 919e41f4b71Sopenharmony_ci } 920e41f4b71Sopenharmony_ci 921e41f4b71Sopenharmony_ci aboutToAppear() { 922e41f4b71Sopenharmony_ci observer.on('navDestinationSwitch', this.getUIContext(), callBackFunc) 923e41f4b71Sopenharmony_ci } 924e41f4b71Sopenharmony_ci 925e41f4b71Sopenharmony_ci aboutToDisappear() { 926e41f4b71Sopenharmony_ci observer.off('navDestinationSwitch', this.getUIContext(), callBackFunc) 927e41f4b71Sopenharmony_ci } 928e41f4b71Sopenharmony_ci 929e41f4b71Sopenharmony_ci build() { 930e41f4b71Sopenharmony_ci Column() { 931e41f4b71Sopenharmony_ci Navigation(this.stack) { 932e41f4b71Sopenharmony_ci Button("push").onClick(() => { 933e41f4b71Sopenharmony_ci this.stack.pushPath({name: "pageOne"}); 934e41f4b71Sopenharmony_ci }) 935e41f4b71Sopenharmony_ci } 936e41f4b71Sopenharmony_ci .title("Navigation") 937e41f4b71Sopenharmony_ci .navDestination(this.PageBuilder) 938e41f4b71Sopenharmony_ci } 939e41f4b71Sopenharmony_ci .width('100%') 940e41f4b71Sopenharmony_ci .height('100%') 941e41f4b71Sopenharmony_ci } 942e41f4b71Sopenharmony_ci} 943e41f4b71Sopenharmony_ci``` 944e41f4b71Sopenharmony_ci 945e41f4b71Sopenharmony_ci## observer.off('navDestinationSwitch')<sup>12+</sup> 946e41f4b71Sopenharmony_ci 947e41f4b71Sopenharmony_cioff(type: 'navDestinationSwitch', context: UIAbilityContext | UIContext, callback?: Callback\<NavDestinationSwitchInfo\>): void 948e41f4b71Sopenharmony_ci 949e41f4b71Sopenharmony_ci取消监听Navigation的页面切换事件。 950e41f4b71Sopenharmony_ci 951e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 952e41f4b71Sopenharmony_ci 953e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 954e41f4b71Sopenharmony_ci 955e41f4b71Sopenharmony_ci**参数:** 956e41f4b71Sopenharmony_ci 957e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 958e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 959e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'navDestinationSwitch',即Navigation的页面切换事件。 | 960e41f4b71Sopenharmony_ci| context | [UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) \| [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面切换事件的范围。 | 961e41f4b71Sopenharmony_ci| callback | Callback\<[NavDestinationSwitchInfo](#navdestinationswitchinfo12)\> | 否 | 需要被注销的回调函数。 | 962e41f4b71Sopenharmony_ci 963e41f4b71Sopenharmony_ci**示例:** 964e41f4b71Sopenharmony_ci 965e41f4b71Sopenharmony_ci参考[observer.on('navDestinationSwitch')](#observeronnavdestinationswitch12)示例。 966e41f4b71Sopenharmony_ci 967e41f4b71Sopenharmony_ci## observer.on('navDestinationSwitch')<sup>12+</sup> 968e41f4b71Sopenharmony_ci 969e41f4b71Sopenharmony_cion(type: 'navDestinationSwitch', context: UIAbilityContext | UIContext, observerOptions: NavDestinationSwitchObserverOptions, callback: Callback\<NavDestinationSwitchInfo\>): void 970e41f4b71Sopenharmony_ci 971e41f4b71Sopenharmony_ci监听Navigation的页面切换事件。 972e41f4b71Sopenharmony_ci 973e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 974e41f4b71Sopenharmony_ci 975e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 976e41f4b71Sopenharmony_ci 977e41f4b71Sopenharmony_ci**参数:** 978e41f4b71Sopenharmony_ci 979e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 980e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 981e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'navDestinationSwitch',即Navigation的页面切换事件。 | 982e41f4b71Sopenharmony_ci| context | [UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) \| [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面切换事件的范围。 | 983e41f4b71Sopenharmony_ci| observerOptions | [NavDestinationSwitchObserverOptions](#navdestinationswitchobserveroptions12) | 是 | 监听选项。 | 984e41f4b71Sopenharmony_ci| callback | Callback\<[NavDestinationSwitchInfo](#navdestinationswitchinfo12)\> | 是 | 回调函数。携带NavDestinationSwitchInfo,返回页面切换事件的信息。 | 985e41f4b71Sopenharmony_ci 986e41f4b71Sopenharmony_ci**示例:** 987e41f4b71Sopenharmony_ci 988e41f4b71Sopenharmony_ci```ts 989e41f4b71Sopenharmony_ci// EntryAbility.ets 990e41f4b71Sopenharmony_ci// 演示 observer.on('navDestinationSwitch', UIAbilityContext, NavDestinationSwitchObserverOptions, callback) 991e41f4b71Sopenharmony_ci// observer.off('navDestinationSwitch', UIAbilityContext, NavDestinationSwitchObserverOptions, callback) 992e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 993e41f4b71Sopenharmony_ciimport { uiObserver as observer, window } from '@kit.ArkUI'; 994e41f4b71Sopenharmony_ciimport { hilog } from "@kit.PerformanceAnalysisKit" 995e41f4b71Sopenharmony_ci 996e41f4b71Sopenharmony_cifunction callBackFunc(info: observer.NavDestinationSwitchInfo) { 997e41f4b71Sopenharmony_ci console.info(`testTag navDestinationSwitch from: ${JSON.stringify(info.from)} to: ${JSON.stringify(info.to)}`) 998e41f4b71Sopenharmony_ci} 999e41f4b71Sopenharmony_ci 1000e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 1001e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 1002e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 1003e41f4b71Sopenharmony_ci observer.on('navDestinationSwitch', this.context, { navigationId: "myNavId" }, callBackFunc); 1004e41f4b71Sopenharmony_ci } 1005e41f4b71Sopenharmony_ci 1006e41f4b71Sopenharmony_ci onDestroy(): void { 1007e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 1008e41f4b71Sopenharmony_ci observer.off('navDestinationSwitch', this.context, { navigationId: "myNavId" }, callBackFunc); 1009e41f4b71Sopenharmony_ci } 1010e41f4b71Sopenharmony_ci 1011e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage): void { 1012e41f4b71Sopenharmony_ci // Main window is created, set main page for this ability 1013e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 1014e41f4b71Sopenharmony_ci 1015e41f4b71Sopenharmony_ci windowStage.loadContent('pages/Index', (err, data) => { 1016e41f4b71Sopenharmony_ci if (err.code) { 1017e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 1018e41f4b71Sopenharmony_ci return; 1019e41f4b71Sopenharmony_ci } 1020e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 1021e41f4b71Sopenharmony_ci }); 1022e41f4b71Sopenharmony_ci } 1023e41f4b71Sopenharmony_ci 1024e41f4b71Sopenharmony_ci onWindowStageDestroy(): void { 1025e41f4b71Sopenharmony_ci // Main window is destroyed, release UI related resources 1026e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 1027e41f4b71Sopenharmony_ci } 1028e41f4b71Sopenharmony_ci 1029e41f4b71Sopenharmony_ci onForeground(): void { 1030e41f4b71Sopenharmony_ci // Ability has brought to foreground 1031e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 1032e41f4b71Sopenharmony_ci } 1033e41f4b71Sopenharmony_ci 1034e41f4b71Sopenharmony_ci onBackground(): void { 1035e41f4b71Sopenharmony_ci // Ability has back to background 1036e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 1037e41f4b71Sopenharmony_ci } 1038e41f4b71Sopenharmony_ci} 1039e41f4b71Sopenharmony_ci``` 1040e41f4b71Sopenharmony_ci 1041e41f4b71Sopenharmony_ci```ts 1042e41f4b71Sopenharmony_ci// Index.ets 1043e41f4b71Sopenharmony_ci// 演示 observer.on('navDestinationSwitch', UIContext, NavDestinationSwitchObserverOptions, callback) 1044e41f4b71Sopenharmony_ci// observer.off('navDestinationSwitch', UIContext, NavDestinationSwitchObserverOptions, callback) 1045e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 1046e41f4b71Sopenharmony_ci 1047e41f4b71Sopenharmony_ci@Component 1048e41f4b71Sopenharmony_cistruct PageOne { 1049e41f4b71Sopenharmony_ci build() { 1050e41f4b71Sopenharmony_ci NavDestination() { 1051e41f4b71Sopenharmony_ci Text("pageOne") 1052e41f4b71Sopenharmony_ci }.title("pageOne") 1053e41f4b71Sopenharmony_ci } 1054e41f4b71Sopenharmony_ci} 1055e41f4b71Sopenharmony_ci 1056e41f4b71Sopenharmony_cifunction callBackFunc(info: observer.NavDestinationSwitchInfo) { 1057e41f4b71Sopenharmony_ci console.info(`testTag navDestinationSwitch from: ${JSON.stringify(info.from)} to: ${JSON.stringify(info.to)}`) 1058e41f4b71Sopenharmony_ci} 1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ci@Entry 1061e41f4b71Sopenharmony_ci@Component 1062e41f4b71Sopenharmony_cistruct Index { 1063e41f4b71Sopenharmony_ci private stack: NavPathStack = new NavPathStack(); 1064e41f4b71Sopenharmony_ci 1065e41f4b71Sopenharmony_ci @Builder 1066e41f4b71Sopenharmony_ci PageBuilder(name: string) { 1067e41f4b71Sopenharmony_ci PageOne() 1068e41f4b71Sopenharmony_ci } 1069e41f4b71Sopenharmony_ci 1070e41f4b71Sopenharmony_ci aboutToAppear() { 1071e41f4b71Sopenharmony_ci observer.on('navDestinationSwitch', this.getUIContext(), { navigationId: "myNavId" }, callBackFunc) 1072e41f4b71Sopenharmony_ci } 1073e41f4b71Sopenharmony_ci 1074e41f4b71Sopenharmony_ci aboutToDisappear() { 1075e41f4b71Sopenharmony_ci observer.off('navDestinationSwitch', this.getUIContext(), { navigationId: "myNavId" }, callBackFunc) 1076e41f4b71Sopenharmony_ci } 1077e41f4b71Sopenharmony_ci 1078e41f4b71Sopenharmony_ci build() { 1079e41f4b71Sopenharmony_ci Column() { 1080e41f4b71Sopenharmony_ci Navigation(this.stack) { 1081e41f4b71Sopenharmony_ci Button("push").onClick(() => { 1082e41f4b71Sopenharmony_ci this.stack.pushPath({name: "pageOne"}); 1083e41f4b71Sopenharmony_ci }) 1084e41f4b71Sopenharmony_ci } 1085e41f4b71Sopenharmony_ci .id("myNavId") 1086e41f4b71Sopenharmony_ci .title("Navigation") 1087e41f4b71Sopenharmony_ci .navDestination(this.PageBuilder) 1088e41f4b71Sopenharmony_ci } 1089e41f4b71Sopenharmony_ci .width('100%') 1090e41f4b71Sopenharmony_ci .height('100%') 1091e41f4b71Sopenharmony_ci } 1092e41f4b71Sopenharmony_ci} 1093e41f4b71Sopenharmony_ci``` 1094e41f4b71Sopenharmony_ci 1095e41f4b71Sopenharmony_ci## observer.off('navDestinationSwitch')<sup>12+</sup> 1096e41f4b71Sopenharmony_ci 1097e41f4b71Sopenharmony_cioff(type: 'navDestinationSwitch', context: UIAbilityContext | UIContext, observerOptions: NavDestinationSwitchObserverOptions, callback?: Callback\<NavDestinationSwitchInfo\>): void 1098e41f4b71Sopenharmony_ci 1099e41f4b71Sopenharmony_ci取消监听Navigation的页面切换事件。 1100e41f4b71Sopenharmony_ci 1101e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1102e41f4b71Sopenharmony_ci 1103e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1104e41f4b71Sopenharmony_ci 1105e41f4b71Sopenharmony_ci**参数:** 1106e41f4b71Sopenharmony_ci 1107e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1108e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1109e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'navDestinationSwitch',即Navigation的页面切换事件。 | 1110e41f4b71Sopenharmony_ci| context | [UIAbilityContext](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md) \| [UIContext](./js-apis-arkui-UIContext.md) | 是 | 上下文信息,用以指定监听页面切换事件的范围。 | 1111e41f4b71Sopenharmony_ci| observerOptions | [NavDestinationSwitchObserverOptions](#navdestinationswitchobserveroptions12) | 是 | 监听选项。 | 1112e41f4b71Sopenharmony_ci| callback | Callback\<[NavDestinationSwitchInfo](#navdestinationswitchinfo12)\> | 否 | 需要被注销的回调函数。 | 1113e41f4b71Sopenharmony_ci 1114e41f4b71Sopenharmony_ci**示例:** 1115e41f4b71Sopenharmony_ci 1116e41f4b71Sopenharmony_ci参考[observer.on('navDestinationSwitch')](#observeronnavdestinationswitch12-1)接口示例。 1117e41f4b71Sopenharmony_ci 1118e41f4b71Sopenharmony_ci## observer.on('tabContentUpdate')<sup>12+</sup> 1119e41f4b71Sopenharmony_ci 1120e41f4b71Sopenharmony_cion(type: 'tabContentUpdate', callback: Callback\<TabContentInfo\>): void 1121e41f4b71Sopenharmony_ci 1122e41f4b71Sopenharmony_ci监听TabContent页面的切换事件。 1123e41f4b71Sopenharmony_ci 1124e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1125e41f4b71Sopenharmony_ci 1126e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1127e41f4b71Sopenharmony_ci 1128e41f4b71Sopenharmony_ci**参数:** 1129e41f4b71Sopenharmony_ci 1130e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1131e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1132e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'tabContentUpdate',即TabContent页面的切换事件。 | 1133e41f4b71Sopenharmony_ci| callback | Callback\<[TabContentInfo](#tabcontentinfo12)\> | 是 | 回调函数。携带TabContentInfo,返回TabContent页面切换事件的信息。 | 1134e41f4b71Sopenharmony_ci 1135e41f4b71Sopenharmony_ci**示例:** 1136e41f4b71Sopenharmony_ci 1137e41f4b71Sopenharmony_ci```ts 1138e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 1139e41f4b71Sopenharmony_ci 1140e41f4b71Sopenharmony_cifunction callbackFunc(info: observer.TabContentInfo) { 1141e41f4b71Sopenharmony_ci console.info('tabContentUpdate', JSON.stringify(info)); 1142e41f4b71Sopenharmony_ci} 1143e41f4b71Sopenharmony_ci 1144e41f4b71Sopenharmony_ci@Entry 1145e41f4b71Sopenharmony_ci@Component 1146e41f4b71Sopenharmony_cistruct TabsExample { 1147e41f4b71Sopenharmony_ci 1148e41f4b71Sopenharmony_ci aboutToAppear(): void { 1149e41f4b71Sopenharmony_ci observer.on('tabContentUpdate', callbackFunc); 1150e41f4b71Sopenharmony_ci } 1151e41f4b71Sopenharmony_ci 1152e41f4b71Sopenharmony_ci aboutToDisappear(): void { 1153e41f4b71Sopenharmony_ci observer.off('tabContentUpdate', callbackFunc); 1154e41f4b71Sopenharmony_ci } 1155e41f4b71Sopenharmony_ci 1156e41f4b71Sopenharmony_ci build() { 1157e41f4b71Sopenharmony_ci Column() { 1158e41f4b71Sopenharmony_ci Tabs() { 1159e41f4b71Sopenharmony_ci TabContent() { 1160e41f4b71Sopenharmony_ci Column().width('100%').height('100%').backgroundColor('#00CB87') 1161e41f4b71Sopenharmony_ci }.tabBar('green').id('tabContentId0') 1162e41f4b71Sopenharmony_ci 1163e41f4b71Sopenharmony_ci TabContent() { 1164e41f4b71Sopenharmony_ci Column().width('100%').height('100%').backgroundColor('#007DFF') 1165e41f4b71Sopenharmony_ci }.tabBar('blue').id('tabContentId1') 1166e41f4b71Sopenharmony_ci 1167e41f4b71Sopenharmony_ci TabContent() { 1168e41f4b71Sopenharmony_ci Column().width('100%').height('100%').backgroundColor('#FFBF00') 1169e41f4b71Sopenharmony_ci }.tabBar('yellow').id('tabContentId2') 1170e41f4b71Sopenharmony_ci 1171e41f4b71Sopenharmony_ci TabContent() { 1172e41f4b71Sopenharmony_ci Column().width('100%').height('100%').backgroundColor('#E67C92') 1173e41f4b71Sopenharmony_ci }.tabBar('pink').id('tabContentId3') 1174e41f4b71Sopenharmony_ci } 1175e41f4b71Sopenharmony_ci .width(360) 1176e41f4b71Sopenharmony_ci .height(296) 1177e41f4b71Sopenharmony_ci .backgroundColor('#F1F3F5') 1178e41f4b71Sopenharmony_ci .id('tabsId') 1179e41f4b71Sopenharmony_ci }.width('100%') 1180e41f4b71Sopenharmony_ci } 1181e41f4b71Sopenharmony_ci} 1182e41f4b71Sopenharmony_ci``` 1183e41f4b71Sopenharmony_ci 1184e41f4b71Sopenharmony_ci## observer.off('tabContentUpdate')<sup>12+</sup> 1185e41f4b71Sopenharmony_ci 1186e41f4b71Sopenharmony_cioff(type: 'tabContentUpdate', callback?: Callback\<TabContentInfo\>): void 1187e41f4b71Sopenharmony_ci 1188e41f4b71Sopenharmony_ci取消监听TabContent页面的切换事件。 1189e41f4b71Sopenharmony_ci 1190e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1191e41f4b71Sopenharmony_ci 1192e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1193e41f4b71Sopenharmony_ci 1194e41f4b71Sopenharmony_ci**参数:** 1195e41f4b71Sopenharmony_ci 1196e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1197e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1198e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'tabContentUpdate',即TabContent页面的切换事件。 | 1199e41f4b71Sopenharmony_ci| callback | Callback\<[TabContentInfo](#tabcontentinfo12)\> | 否 | 需要被注销的回调函数。 | 1200e41f4b71Sopenharmony_ci 1201e41f4b71Sopenharmony_ci**示例:** 1202e41f4b71Sopenharmony_ci 1203e41f4b71Sopenharmony_ci参考[observer.on('tabContentUpdate')](#observerontabcontentupdate12)接口示例。 1204e41f4b71Sopenharmony_ci 1205e41f4b71Sopenharmony_ci## observer.on('tabContentUpdate')<sup>12+</sup> 1206e41f4b71Sopenharmony_ci 1207e41f4b71Sopenharmony_cion(type: 'tabContentUpdate', options: ObserverOptions, callback: Callback\<TabContentInfo\>): void 1208e41f4b71Sopenharmony_ci 1209e41f4b71Sopenharmony_ci监听TabContent页面的切换事件。 1210e41f4b71Sopenharmony_ci 1211e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1212e41f4b71Sopenharmony_ci 1213e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1214e41f4b71Sopenharmony_ci 1215e41f4b71Sopenharmony_ci**参数:** 1216e41f4b71Sopenharmony_ci 1217e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1218e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1219e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'tabContentUpdate',即TabContent页面的切换事件。 | 1220e41f4b71Sopenharmony_ci| options | [ObserverOptions](#observeroptions12) | 是 | 指定监听的Tabs组件的id。 | 1221e41f4b71Sopenharmony_ci| callback | Callback\<[TabContentInfo](#tabcontentinfo12)\> | 是 | 回调函数。携带TabContentInfo,返回TabContent页面切换事件的信息。 | 1222e41f4b71Sopenharmony_ci 1223e41f4b71Sopenharmony_ci**示例:** 1224e41f4b71Sopenharmony_ci 1225e41f4b71Sopenharmony_ci```ts 1226e41f4b71Sopenharmony_ciimport { uiObserver as observer } from '@kit.ArkUI'; 1227e41f4b71Sopenharmony_ci 1228e41f4b71Sopenharmony_cifunction callbackFunc(info: observer.TabContentInfo) { 1229e41f4b71Sopenharmony_ci console.info('tabContentUpdate', JSON.stringify(info)); 1230e41f4b71Sopenharmony_ci} 1231e41f4b71Sopenharmony_ci 1232e41f4b71Sopenharmony_ci@Entry 1233e41f4b71Sopenharmony_ci@Component 1234e41f4b71Sopenharmony_cistruct TabsExample { 1235e41f4b71Sopenharmony_ci 1236e41f4b71Sopenharmony_ci aboutToAppear(): void { 1237e41f4b71Sopenharmony_ci observer.on('tabContentUpdate', { id: 'tabsId' }, callbackFunc); 1238e41f4b71Sopenharmony_ci } 1239e41f4b71Sopenharmony_ci 1240e41f4b71Sopenharmony_ci aboutToDisappear(): void { 1241e41f4b71Sopenharmony_ci observer.off('tabContentUpdate', { id: 'tabsId' }, callbackFunc); 1242e41f4b71Sopenharmony_ci } 1243e41f4b71Sopenharmony_ci 1244e41f4b71Sopenharmony_ci build() { 1245e41f4b71Sopenharmony_ci Column() { 1246e41f4b71Sopenharmony_ci Tabs() { 1247e41f4b71Sopenharmony_ci TabContent() { 1248e41f4b71Sopenharmony_ci Column().width('100%').height('100%').backgroundColor('#00CB87') 1249e41f4b71Sopenharmony_ci }.tabBar('green').id('tabContentId0') 1250e41f4b71Sopenharmony_ci 1251e41f4b71Sopenharmony_ci TabContent() { 1252e41f4b71Sopenharmony_ci Column().width('100%').height('100%').backgroundColor('#007DFF') 1253e41f4b71Sopenharmony_ci }.tabBar('blue').id('tabContentId1') 1254e41f4b71Sopenharmony_ci 1255e41f4b71Sopenharmony_ci TabContent() { 1256e41f4b71Sopenharmony_ci Column().width('100%').height('100%').backgroundColor('#FFBF00') 1257e41f4b71Sopenharmony_ci }.tabBar('yellow').id('tabContentId2') 1258e41f4b71Sopenharmony_ci 1259e41f4b71Sopenharmony_ci TabContent() { 1260e41f4b71Sopenharmony_ci Column().width('100%').height('100%').backgroundColor('#E67C92') 1261e41f4b71Sopenharmony_ci }.tabBar('pink').id('tabContentId3') 1262e41f4b71Sopenharmony_ci } 1263e41f4b71Sopenharmony_ci .width(360) 1264e41f4b71Sopenharmony_ci .height(296) 1265e41f4b71Sopenharmony_ci .backgroundColor('#F1F3F5') 1266e41f4b71Sopenharmony_ci .id('tabsId') 1267e41f4b71Sopenharmony_ci }.width('100%') 1268e41f4b71Sopenharmony_ci } 1269e41f4b71Sopenharmony_ci} 1270e41f4b71Sopenharmony_ci``` 1271e41f4b71Sopenharmony_ci 1272e41f4b71Sopenharmony_ci## observer.off('tabContentUpdate')<sup>12+</sup> 1273e41f4b71Sopenharmony_ci 1274e41f4b71Sopenharmony_cioff(type: 'tabContentUpdate', options: ObserverOptions, callback?: Callback\<TabContentInfo\>): void 1275e41f4b71Sopenharmony_ci 1276e41f4b71Sopenharmony_ci取消监听TabContent页面的切换事件。 1277e41f4b71Sopenharmony_ci 1278e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1279e41f4b71Sopenharmony_ci 1280e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 1281e41f4b71Sopenharmony_ci 1282e41f4b71Sopenharmony_ci**参数:** 1283e41f4b71Sopenharmony_ci 1284e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 1285e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1286e41f4b71Sopenharmony_ci| type | string | 是 | 监听事件,固定为'tabContentUpdate',即TabContent页面的切换事件。 | 1287e41f4b71Sopenharmony_ci| options | [ObserverOptions](#observeroptions12) | 是 | 指定监听的Tabs组件的id。 | 1288e41f4b71Sopenharmony_ci| callback | Callback\<[TabContentInfo](#tabcontentinfo12)\> | 否 | 需要被注销的回调函数。 | 1289e41f4b71Sopenharmony_ci 1290e41f4b71Sopenharmony_ci**示例:** 1291e41f4b71Sopenharmony_ci 1292e41f4b71Sopenharmony_ci参考[observer.on('tabContentUpdate')](#observerontabcontentupdate12-1)接口示例。