HarmonyOS 4.0——跨模块跳转

288 阅读1分钟

entry模块跳转SharedLibrary模块

注:SharedLibrary模块没有ability文件,因此不能使用want跳转,需使用路由跳转。

背景

项目最外层文件夹右键模块

image.png

选择SharedLibrary模块

image.png

entry模块跳转Testlibrary模块的Index页面,而Testlibrary模块没有ability.

以下是目录结构示意:

image.png

解决思路

使用router.pushUrl方法,参数是@bundle:com.my.application/动态库名称/ets/pages/页面名称

Demo

  1. 获取App的bundleName

AppScope/app.json5 image.png

  1. entry模块里写跳转方法

src/main/ets/pages/Index.ets

Button('跳转Test模块')
  .onClick(() => {
    // @bundle:com.my.application/动态库名称/ets/pages/页面名称
    router.pushUrl({ url: '@bundle:com.example.hm/Testlibrary/ets/pages/Index'})
      })
  })

跨模块跳转成功啦!!!

entry模块跳转EmptyLibrary模块

注:EmptyLibrary模块有ability文件,可使用want进行模块跳转。

背景

项目最外层文件夹右键模块

image.png

选择EmptyLibrary模块

image.png

entry模块跳转Test3模块的Index页面,而Test3模块有ability.

以下是目录结构示意:

image.png

解决思路

使用显示want方法,通过startAbility调起指定模块。

Demo

  1. 获取App的bundleName

AppScope/app.json5 image.png

  1. entry模块里写跳转方法

src/main/ets/pages/Index.ets

Button('跳转Test3模块')
  .onClick(() => {
    let want: Want = { 
      deviceId: '',
      bundleName: 'com.example.hm',
      moduleName: 'Test3',
      abilityName: 'TestAbility3',
    }
    //   跳转
    this.context.startAbility(want, (error) => {
      // 显式拉起Ability,通过bundleName、abilityName和moduleName可以唯一确定一个Ability
      console.log('Tag', 'error.code = ' + error.code)
    });
  })

跨模块跳转成功啦!!!