HarmonyOS Router 1.1.0 Automates Route Setup: One Plugin, No More Manual Annotations
highlight: a11y-dark theme: channing-cyan
Foreword
Since version 1.0.0 went live in September 2024, I have promoted the use of router with annotations, letting developers complete page jumps with one annotation and one line of code. Both then and now, it is still the most convenient way to use it in HarmonyOS development.
Execute jump:
startPage("entry_test")
Related page (must be exported):
@RouterPath("entry_test")
@ComponentV2
export struct Test{
build(){
Column(){}
}
}
The above completes route registration and jump, which is already relatively concise. But it has two problems. First, the route must be in pure string form, which obviously doesn't meet daily development needs, because we need a route management utility class to manage it separately. When there are many modules, there may be multiple route management classes. Second, the module prefix must be carried because the internal module lookup requires it. However, this conflicts with actual development, for instance when the route identifier is intended to be shared across multiple ends. Managing route identifiers that way is inconvenient. Though the prefix can be added internally on the HarmonyOS side without affecting external consistency, manual configuration during development is still inconvenient.
To address the first problem, the later release allowed each module to create a route management class under its root path to manage the entire module. For example, under the entry module, you can create any management class, like EntryRouter:
The route writing also follows the rule of module name + custom route alias.
export class EntryRouter {
static EntryTest= "entry_test"//测试页面
}
Usage within that module stays the same; get it directly:
@RouterPath(EntryRouter.EntryTest)
@Component
export struct TestPage {
build() {
//UI components
}
}
Jump within the module:
startPage(EntryRouter.EntryTest)
Each module managing its own routes seems clear and maintainable, but it has serious limitations. For example, when jumping across modules, how do you get the route? Defining it in both modules solves the problem but creates redundant code. Moreover, the second problem, the prefix, still exists.
In addition to these two problems, every time you write a UI page, you still have to manually write the @RouterPath annotation and manually add imports. These tedious steps greatly reduce development efficiency.
The problems above forced me to re-examine the router component. Its original intent was to help developers improve efficiency, but while it improved some efficiency, it also introduced problems. So optimization had to be put on the agenda.
After two days of continuous optimization, version 1.1.0 was released. This update brings a brand-new route usage method and a brand-new supporting plugin, making router both easy to use and easy to maintain.
Router component link:
https://ohpm.openharmony.cn/#/cn/detail/@abner%2Frouter
Route Management Module
I had already determined the route management module concept long ago and implemented it in real projects. It just hadn't been put into the router component. This update makes the concept explicit.
If your project adopts modular/componentized development, the route management module fits perfectly. You only need to create a separate router static shared package. Because it manages the routes of all modules, you declare it as a global dependency in the project's oh-package.json5 file. Remember, it's the project-level oh-package.json5 file, not a specific module's.
Declare the dependency in the project-level oh-package.json5:
After the above steps, the route management module is complete. From then on, all module page routes are configured in the route module. When writing routes or looking up routes, you only need to focus on this module. Of course, since we're managing routes, there must be constraints. All routes in pages should be configured here; no one should be unconstrained or arbitrary, otherwise it defeats the purpose.
To solve the team's route management problem, there's no need to manually create routes for the route management module. With the later plugin, all pages are automatically configured there one by one.
Plugin Page Creation
The previous plugin only supported pure string route configuration. Starting with version 1.1.0, a third creation mode was added: Router Module Page. Just right-click where you want to create the page and select New -> Router Page -> Router Module Page.
Enter the UI page name and the custom route name, then press Enter or click OK.
A complete UI page with route registration is now created. The whole process requires no manual configuration of routes; you just write the UI page normally.
import { RouterPath } from "@abner/router";
import { RouterManager } from "router";
/**
*AUTHOR:AbnerMingdeMacBook-Pro.local
*DATE:2026/11/9
*INTRODUCE:
*/
@RouterPath(RouterManager.share.USER_INFO)
@Component
export struct UserInfoPage {
@State message: string = 'Hello UserInfoPage';
build() {
RelativeContainer() {
Text(this.message)
.fontWeight(FontWeight.Bold)
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.onClick(() => {
this.message = 'Welcome';
})
}
.height('100%')
.width('100%')
}
}
The page created above is automatically configured into the router management module, with no manual configuration needed.
RouterManager class:
import { MineConstants } from "./constants/MineConstants"
import { EntryConstants } from "./constants/EntryConstants"
import { getRouterModuleName } from "@abner/router"
import { ShareConstants } from "./constants/ShareConstants"
/**
*AUTHOR:AbnerMing
*DATE:2026/11/9
*INTRODUCE:路由管理,如果想要路由不写前缀,需要在这里进行配置
*/
export class RouterManager {
static readonly mine = getRouterModuleName(MineConstants, "mine")
static readonly entry = getRouterModuleName(EntryConstants, "entry")
static readonly share = getRouterModuleName(ShareConstants, "share")
}
ShareConstants class:
/**
*AUTHOR:AbnerMing
*DATE:2026/11/9
*INTRODUCE:share模块路由常量配置
*/
export class ShareConstants {
static MESSAGE: string = "message"
static USER_INFO: string = "user_info"
}
Routes are all divided by module for easy lookup and modification, and they are fully custom-defined, with no need to manually configure prefixes.
Subsequent jumps use the RouterManager class, followed by the module, then the custom route, with a clear structure at a glance.
startPage(RouterManager.share.USER_INFO)
Summary
The feature of this update is the introduction of a route management module and the updated supporting plugin usage. The plugin drives automatic route configuration to free developers from manual configuration. If there are any issues during use, feedback is welcome, and fixes will be made from time to time.
Top 1 from juejin.cn, machine-translated. The original thread is authoritative.
Impressive!