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=''
1.在ability中声明并设置 LocalStorage 的值,在loadContent 时将LocalStorage 做为参数传
2.在ets中接受参数,如果ts中没有设置,就会使用默认值,pageType的默认值是空字