In large or complex systems, services are often split into multiple modules, each as a HAR or HSP. To navigate between these modules, such as from the home page to a login page, there are three solutions:
Reference: Page Routing (@ohos.router)
The following example describes how to navigate from the entry module's NavigationPage component to the Login module's LoginPage component:
@Component
export struct LoginPage {
@Consume('pathStack') pathStack: NavPathStack;
@State message: string = 'Login Page';
build() {
NavDestination() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
.height('100%')
}
.onBackPressed(() => {
this.pathStack.pop();
return true;
})
}
} {
// ...
"dependencies": {
"@ohos/login": "file:../LoginModule"
}
} // Import a custom component of the Login module
import { LoginPage } from '@ohos/login';
@Entry
@Component
struct NavigationPage {
@Provide('pathStack') pathStack: NavPathStack = new NavPathStack();
@Builder
pageMap(name: string) {
if (name === 'loginPage') {
LoginPage()
}
}
build() {
Navigation(this.pathStack) {
Button('jump to login page')
.onClick(() => {
// The second parameter of NavPathInfo is a custom parameter that can be used for message transfer
let pathInfo: NavPathInfo = new NavPathInfo('loginPage', new Object());
this.pathStack.pushDestination(pathInfo, true);
})
}
.navDestination(this.pageMap)
}
} While solution 2 enables cross-module navigation, with increasing inter-module transitions, it can cause complex dependencies between modules and even potential circular dependencies among multiple HAR/HSP files. To address the strong coupling between modules and enhance page loading performance, cross-package routing is recommended.