若开发者需要跳转分包页面时,需要使用以下api进行跳转:router.pushUrl和router.replaceUrl。关于路由,详见页面路由。
以上述工程为例,若开发者想在entry模块中跳转至library模块中的menu页面(路径为:library/src/main/ets/pages/menu.ets),那么可以在使用以下代码(entry模块下的Index.ets,路径为:entry/src/main/ets/pages/Index.ets):
- import { BusinessError } from '@ohos.base';
- import router from '@ohos.router';
-
- @Entry
- @Component
- struct Index {
- @State message: string = 'Hello World';
-
- build() {
- Row() {
- Column() {
- Text(this.message)
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
-
- Button('click to library')
- .onClick(()=>{
- router.pushUrl({
- url: '@bundle:com.atomicservice.123456789/library/ets/pages/menu'
- }).then(() => {
- console.info('push page success');
- }).catch((err: BusinessError) => {
- console.error(`pushUrl failed, code is ${err.code}, message is ${err.message}`);
- })
- })
- }
- .width('100%')
- }
- .height('100%')
- }
- }
其中router.pushUrl方法的入参中url的内容为:
- '@bundle:com.atomicservice.123456789/library/ets/pages/menu'
url内容的模板为:
- '@bundle:包名(bundleName)/模块名(moduleName)/路径/页面所在的文件名(不加.ets后缀)'