跪拜 Guibai
← All articles
HarmonyOS · Android · iOS

HarmonyOS Router 1.1.0 Automates Route Setup: One Plugin, No More Manual Annotations

By 程序员一鸣 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams on modular HarmonyOS projects can now centralize all route definitions in one package and let the IDE generate registration code, which removes a recurring source of boilerplate and prefix mismatches. The payoff depends on enforcing the single-module rule, since the plugin only works if every page route goes through it.

Summary

Router 1.1.0 for HarmonyOS replaces the old string-based route registration with a dedicated route management module and a plugin that auto-generates both the page and its route entry. Pages created via the IDE's 'Router Module Page' option automatically register in a shared router package, so developers no longer hand-write @RouterPath annotations or import statements.

The update also solves the module-prefix problem. Routes in the management module are defined without prefixes, and the RouterManager class calls getRouterModuleName to map module constants to their actual route names. Cross-module navigation now goes through a single, centralized RouterManager, which keeps the full route map searchable in one place.

The catch is discipline. The author stresses that every route must live in the router module, or the auto-generation and central lookup break down. Teams that adopt this need to enforce the convention across all modules.

Takeaways
— Router 1.1.0 adds a third route creation mode: Router Module Page, accessible from IDE right-click New > Router Page.
— The router management module is a standalone static shared package that declares all module routes; it is added as a global dependency in the project-level oh-package.json5, not in any module's file.
— The plugin auto-creates a UI page with @RouterPath(RouterManager.share.USER_INFO) and registers the route in RouterManager, so no manual annotation or import is needed.
— RouterManager uses getRouterModuleName(Constants, "moduleName") to bind each module's constant set to a route name, stripping the module prefix from actual route strings.
— Route identifiers in the management module are plain constants like "user_info" under module-specific classes such as ShareConstants, EntryConstants, and MineConstants.
— Navigation becomes startPage(RouterManager.share.USER_INFO), going through the central manager.
Conclusions

Centralizing routes in a single module turns route management into a team convention; the author explicitly warns against some developers going rogue, so the real adoption barrier is process, not code.

Keeping route strings free of module prefixes while injecting prefixes internally via getRouterModuleName preserves cross-platform identifier reuse, which was a stated pain point.

Removing manual annotation and import steps shifts work to the IDE plugin, so route consistency depends on everyone using the same generation path.

Concepts & terms
@RouterPath
Annotation from the @abner/router library that marks a struct as a routable page. The string or constant inside it becomes the page's route identifier.
Route management module
A dedicated static shared package that declares all routes for every module in the project. In this update it centralizes route lookup and eliminates manual per-page registration.
getRouterModuleName
A helper from @abner/router that takes a constants class and a module name (e.g., "share") and returns a prefixed route string, letting developer-facing identifiers stay free of module prefixes.
oh-package.json5
HarmonyOS's package manifest (similar to package.json). The project-level file declares dependencies that apply across all modules, including the router management module.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗