1/* 2 * Copyright (c) 2023 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 */ 15 16import router from '@ohos.router'; 17import logger from '../utils/Logger'; 18 19@Component 20export struct DynamicHome { 21 @Provide('pathInfos') pageInfos: NavPathStack = new NavPathStack(); 22 @State active: boolean = false; 23 @BuilderParam PageOneLoader: () => void; 24 25 @Builder 26 PageMap(name: string) { 27 if (name === 'pageOne') { 28 this.PageOneLoader(); 29 } 30 } 31 32 build() { 33 Navigation(this.pageInfos) { 34 Column() { 35 Button($r('app.string.home_button_back'), { stateEffect: true, type: ButtonType.Capsule }) 36 .width('80%') 37 .height(40) 38 .margin(20) 39 .onClick(() => { 40 router.back(); 41 }) 42 Button($r('app.string.home_button_page_one'), { stateEffect: true, type: ButtonType.Capsule }) 43 .width('80%') 44 .height(40) 45 .margin(20) 46 .onClick(() => { 47 this.onEntryClick(); 48 }) 49 } 50 }.title($r('app.string.home_title')).navDestination(this.PageMap) 51 } 52 53 async loadPageOne(key: String): Promise<void> { 54 if (key === "pageOne") { 55 let PageObj: Record<string, () => void> = await import("../model/PageOneLoader"); 56 this.PageOneLoader = PageObj.PageOneLoader; 57 } 58 } 59 60 // 触发动态加载 61 private onEntryClick(): void { 62 try { 63 this.loadPageOne('pageOne'); 64 this.pageInfos.clear(); 65 this.pageInfos.pushPathByName('pageOne', ''); 66 logger.info("DynamicImport Success"); 67 } catch (error) { 68 logger.info("DynamicImport Fail"); 69 } 70 } 71}