HarmonyOS 鸿蒙Next:Applinking冷启动情况,用windowStage.load动态加载启动页时能否传递参数到加载的页面内,是否有实现的样例代码
HarmonyOS 鸿蒙Next:Applinking冷启动情况,用windowStage.load动态加载启动页时能否传递参数到加载的页面内,是否有实现的样例代码
Applinking冷启动情况,用windowStage.load动态加载启动页时 能否传递参数到加载的页面内,是否有实现的样例代码
2 回复
可以参考 1.在ability中声明并设置 LocalStorage 的值,在loadContent 时将LocalStorage 做为参数传,
export default class EntryAbility extends UIAbility {
private localStorage:LocalStorage=new LocalStorage()
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
let targetPageType=want?.parameters?.['targetPageType']
this.localStorage.setOrCreate('targetPageType',targetPageType)
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index',this.localStorage, (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
});
}
2.在ets中接受参数,如果ts中没有设置,就会使用默认值,pageType的默认值是空字
let storage=LocalStorage.GetShared()
@Entry(storage)
struct Index{
@LocalStorageLink('targetPageType') pageType:string=''