#鸿蒙通关秘籍#如何在PageAbility中实现指定页面启动?-鸿蒙开发者社区-51CTO.COM

#鸿蒙通关秘籍#如何在PageAbility中实现指定页面启动?

HarmonyOS
2024-12-11 12:43:02
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
NetNerd

在指定页面启动PageAbility时,可以通过want中的parameters参数传递页面信息。以下示例展示了如何传递页面信息并在目标端处理:

调用方PageAbility中使用startAbility方法:

javascript import featureAbility from '@ohos.ability.featureAbility'; import Want from '@ohos.app.ability.Want'; import Logger from '../../utils/Logger';

const TAG: string = 'PagePageAbilityFirst';

(async () => { let wantInfo = { bundleName: 'com.samples.famodelabilitydevelop', abilityName: 'com.samples.famodelabilitydevelop.PageAbilitySingleton', parameters: { page: 'pages/second' } }; featureAbility.startAbility({ want: wantInfo }).then((data) => { Logger.info(TAG, restartAbility success: ${data}); }); })();

目标端PageAbility的onNewWant方法中处理:

javascript import Want from '@ohos.app.ability.Want'; import featureAbility from '@ohos.ability.featureAbility'; import { GlobalContext } from '../utils/GlobalContext';

class PageAbilitySingleton { onNewWant(want) { featureAbility.getWant().then((want) => { GlobalContext.getContext().setObject('newWant', want); }); } } export default new PageAbilitySingleton();

通过这些方法,可以在鸿蒙应用中实现精准的页面跳转与启动。

分享
微博
QQ
微信
回复
2024-12-11 13:45:28
相关问题