If NavDestination of a Navigation-based redirection is in a different HSP package that is not depended on by the main package, you must use NavPushPathHelper for routing because only the main package is downloaded and installed when the atomic service is run for the first time. NavPushPathHelper encapsulates all route APIs of NavPathStack. It holds a NavPathStack object and uses the encapsulated API to determine whether an HSP package exists. If not, it dynamically downloads the package. After obtaining the result, it calls the corresponding API of NavPathStack to add the specified NavDestination to the stack.
Page Navigation
The following uses NavDestination in ProductList.ets of the product package as an example:
Create a NavPathStack object in the ProductList component, obtain NavPathStack to which NavDestination belongs in the onReady event of NavDestination, and initialize NavPathStack.
@Component
struct ProductList {
pathStack: NavPathStack = new NavPathStack()
build() {
NavDestination() {
Text('Product list')
}
.title('Product List')
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack
})
}
} Create the entry builder function ProductListBuilder in the ProductList.ets file to obtain a ProductList component.
@Builder
export function ProductListBuilder() {
ProductList()
}
@Component
struct ProductList {
pathStack: NavPathStack = new NavPathStack()
build() {
NavDestination() {
Text('Product list')
}
.title('Product List')
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack
})
}
} Create the route_map.json file in resources/base/profile of the product sub-package, and add the following information to the file:
{
"routerMap": [
{
"name": "ProductList",
"pageSourceFile": "src/main/ets/pages/ProductList.ets",
"buildFunction": "ProductListBuilder",
"data": {
"description" : "this is ProductList"
}
}
]
} Define the routerMap field under the module tag in the module.json5 configuration file of the product sub-package to point to the defined routing table configuration file route_map.json.
"routerMap": "$profile:route_map"
Notice
The "pages": "$profile:main_pages" line under module must be deleted. Otherwise, an exception occurs during routing on the application after it is released. Consequently, a white screen is displayed.
To navigate to the ETS file of ProductList from a different sub-package, import NavPushPathHelper and instantiate it by passing an instance of NavPathStack to its constructor.
import { NavPushPathHelper } from '@kit.ArkUI'
pathStack: NavPathStack = new NavPathStack()
// Example of generating NavPushPathHelper based on NavPathStack.
helper: NavPushPathHelper = new NavPushPathHelper(this.pathStack)
Neither the main package nor a sub-package navigating to another sub-package needs to directly depend on the sub-package containing the target page.
Call the page navigation API of NavPushPathHelper to navigate to the sub-page in ProductList.ets.
this.helper.pushPath('product', { name: 'ProductList' }, false)
.then(() => {
console.error('[pushPath]success.');
})
.catch((error: BusinessError) => {
console.error(`[pushPath]failed, error code = ${error.code}, error.message = ${error.message}.`);
});
The main package does not directly depend on the package of the target page. Therefore, DevEco Studio does not automatically install the package on the mobile phone during running or debugging. You are required to click Run > Edit Configurations > entry > Deploy Multi Hap, select Deploy Multi Hap Packages, and select to load the subpackage where the target page is located.
Other Page Operations
Use the corresponding NavPathStack methods directly to handle page routing operations within a sub-package, including page return, replace, delete, parameter fetching, and routing interception. For details, see APIs.