1/* 2* Copyright (C) 2024 Huawei Device Co., Ltd. 3* Licensed under the Apache License, Version 2.0 (the "License"); 4* you may not use this file except in compliance with the License. 5* You may obtain a copy of the License at 6* 7* http://www.apache.org/licenses/LICENSE-2.0 8* 9* Unless required by applicable law or agreed to in writing, software 10* distributed under the License is distributed on an "AS IS" BASIS, 11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12* See the License for the specific language governing permissions and 13* limitations under the License. 14*/ 15export class RouterModule { 16 static builderMap: Map<string, WrappedBuilder<[object]>> = new Map<string, WrappedBuilder<[object]>>(); 17 static routerMap: Map<string, NavPathStack> = new Map<string, NavPathStack>(); 18 19 // 通过名称注册builder 20 public static registerBuilder(builderName: string, builder: WrappedBuilder<[object]>): void{ 21 RouterModule.builderMap.set(builderName, builder); 22 } 23 24 // 通过名称获取builder 25 public static getBuilder(builderName: string): WrappedBuilder<[object]>{ 26 let builder = RouterModule.builderMap.get(builderName); 27 if(!builder){ 28 let msg = "not found builder"; 29 console.info(msg + builderName); 30 } 31 return builder as WrappedBuilder<[object]>; 32 } 33 34 // 通过名称注册router 35 public static createRouter(routerName: string, router: NavPathStack): void{ 36 RouterModule.routerMap.set(routerName, router); 37 } 38 39 // 通过名称获取router 40 public static getRouter(routerName: string): NavPathStack{ 41 let router = RouterModule.routerMap.get(routerName); 42 if(!router){ 43 let msg = "not found router"; 44 console.info(msg + routerName); 45 } 46 return router as NavPathStack; 47 } 48 49 // 通过获取页面栈跳转到指定页面 50 public static push(url:string): void{ 51 // 分解url 52 let params = url.split("-"); 53 const path = params[0]; 54 const routerName = params[1]; 55 const pageName = params[2]; 56 57 // 动态引入要跳转的页面 58 switch (path){ 59 case "harA/src/main/ets/components/mainpage/HarA": 60 import("harA/src/main/ets/components/mainpage/HarA"); 61 break; 62 case "harB/src/main/ets/components/mainpage/HarB": 63 import("harB/src/main/ets/components/mainpage/HarB"); 64 break; 65 } 66 // 查找到对应的路由栈进行跳转 67 RouterModule.getRouter(routerName).pushPathByName(pageName, null); 68 } 69 70 // 通过获取页面栈并pop 71 public static pop(routerName:string): void { 72 // 查找到对应的路由栈进行pop 73 RouterModule.getRouter(routerName).pop(); 74 } 75 76 // 通过获取页面栈并将其清空 77 public static clear(routerName:string): void { 78 // 查找到对应的路由栈进行pop 79 RouterModule.getRouter(routerName).clear(); 80 } 81} 82