关于har和hsp的热重载使用

关于har和hsp的热重载使用

HarmonyOS
2024-05-22 22:28:23
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
一意孤行的

DevEco Studio提供Hot Reload(热重载)能力,支持开发者在真机上运行/调试运行应用时,修改代码并保存后无需重启应用,在真机上即可使用最新的代码,帮助开发者更快速地进行调试。

使用的核心API

核心代码解释

entry模块代码

import {hspreturn} from "hsp1/Index" 
import {harreturn} from "har1/Index" 
import router from '@ohos.router'; 
import {Indexhsp1} from "hsp1/Index" 
import {Indexhar1} from "har1" 
  
// import { a } from './Cal'; 
  
@Entry 
@Component 
struct Index { 
  @State message3: string = '测试是更新entry的资源'; 
  @State message1: string = '测试是否拿到hsp1的资源'; 
  @State message2: string = '测试是否拿到har1的资源'; 
  build() { 
    Row() { 
      Column() { 
        Text(this.message3) 
  
        Button('调用entry模块下的方法') 
          .onClick(()=>{ 
            let f=new a('张三') 
            this.message3=f.entrys() 
          }) 
        Button("恢复") 
          .onClick(()=>{ 
            this.message3='测试是更新entry的资源' 
          }) 
        Text(this.message1) 
          .fontSize(30) 
          .fontWeight(FontWeight.Bold) 
        Button("拿hsp1的资源") 
          .onClick(()=>{ 
            this.message1=hspreturn() 
            let m=new a('zhd') 
          }) 
        Button("恢复") 
          .onClick(()=>{ 
            this.message1='拿hsp1的资源ffddddd' 
          }) 
        Text(this.message2) 
          .fontSize(30) 
          .fontWeight(FontWeight.Bold) 
        Button("拿har1的资源") 
          .onClick(()=>{ 
            this.message2=harreturn() 
          }) 
        Button("恢复") 
          .onClick(()=>{ 
            this.message2='测试是否拿到har1的资源' 
          }) 
        Button("我是热过载测试121") 
          .backgroundColor(Color.Pink) 
          .width("200") 
        Indexhar1() 
        Indexhsp1() 
  
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
  
export  class a{ 
  name:string 
  
  constructor(name) { 
    this.name=name 
  } 
  entrys():string{ 
    return this.name+"11" 
  
  } 
  
} 
  
export  class a{ 
  name:string 
  
  constructor(name) { 
    this.name=name 
  } 
  entrys():string{ 
    return this.name+"111" 
  
  } 
  
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.

har模块代码:

@Entry 
@Component 
export  struct Indexhar1 { 
  @State message: string = '这里是har21包里的页面'; 
  
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(10) 
          .fontWeight(FontWeight.Bold) 
        Button("har1") 
          .backgroundColor(Color.Orange) 
  
      } 
      .width('20%') 
    } 
    .height('20%') 
  } 
} 
export function harreturn():string { 
  return "这是har里的函数454" 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

hsp模块代码:

@Entry 
@Component 
export  struct Indexhsp1 { 
  @State message: string = '这里是hsp1包里的页面'; 
  
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(10) 
          .fontWeight(FontWeight.Bold) 
        Button("hsp1") 
          .backgroundColor(Color.Black) 
  
      } 
      } 
    } 
  } 
export function hspreturn():string { 
    return "这是hsp里的函数22" 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

实现效果

修改entry模块的button颜色,点击图标,即可热重载。

ide不需要重新推包,并且重新打开应用,即可完成更新,消耗时间少。

结果:

1.修改entry模块UI,可以触发热过载。

2.修改har中UI或者同时修改har和entry下的UI资源,可以触发热过载。

3.修改hsp中UI资源,不可以触发热过载。

4.同时修改hsp和entry下的UI资源,可以触发热过载,但只会更新entry模块下的。

5.修改函数类方法等都不能热过载。

注明适配的版本信息

IDE:DevEco    Studio 4.0.3.600

SDK:HarmoneyOS    4.0.10.11

分享
微博
QQ
微信
回复
2024-05-23 16:12:36
相关问题
请问重载如何启动?
366浏览 • 0回复 待解决
Flutter 重载方法有哪些?
1080浏览 • 1回复 待解决
HARHSP使用场景区分
1507浏览 • 1回复 待解决
HarmonyOS harhsp选择
620浏览 • 1回复 待解决
4.0release不支持重载
3691浏览 • 1回复 待解决
目前开发工具支持重载
330浏览 • 1回复 待解决
HARHSP,分别是如何引入
2255浏览 • 1回复 待解决
重载该如何实现?有人知道吗?
1370浏览 • 1回复 待解决
HarmonyOS ArkTS 关于重载实现方式
818浏览 • 1回复 待解决
hsphar包互相转换方案
2318浏览 • 1回复 待解决
harhsp转换,有好方案吗?
1621浏览 • 1回复 待解决
HarmonyOS harhsp
766浏览 • 1回复 待解决
HarmonyOS 集成态HSPhar有什么区别
776浏览 • 1回复 待解决
从HAP拆包中,如何区分是HARHSP
2916浏览 • 1回复 待解决
HarmonyOS hsp调用har代码
998浏览 • 1回复 待解决
HAR 是否可以依赖 HSP?
1204浏览 • 1回复 待解决
如何去实现部分har页面的更新
1140浏览 • 1回复 待解决
#鸿蒙通关秘籍#HSPHAR有啥不一样?
872浏览 • 1回复 待解决