关于har和hsp的热重载使用

关于har和hsp的热重载使用

HarmonyOS
2024-05-22 22:28:23
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
lilyglp

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" 
  
  } 
  
}

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" 
}

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" 
}

实现效果

修改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
相关问题
4.0release不支持重载
756浏览 • 1回复 待解决
HARHSP,分别是如何引入
391浏览 • 1回复 待解决
重载该如何实现?有人知道吗?
301浏览 • 1回复 待解决
harhsp转换,有好方案吗?
239浏览 • 1回复 待解决
hsphar包互相转换方案
535浏览 • 1回复 待解决
从HAP拆包中,如何区分是HARHSP
750浏览 • 1回复 待解决
关于Grid容器WaterFlow使用问题
426浏览 • 1回复 待解决
IDE无法新建harhsp模版
289浏览 • 1回复 待解决
如何理解HAP、HARHSP三者之间关系
1360浏览 • 1回复 待解决
如何将har库迁移为hsp
371浏览 • 1回复 待解决
关于导入图片使用权限问题
359浏览 • 1回复 待解决
能否提供命令行,手动编译har或者hsp
246浏览 • 1回复 待解决
鸿蒙插件化更新机制
1174浏览 • 1回复 待解决
关于onGestureJudgeBegin使用
271浏览 • 1回复 待解决
ArkTS类方法是否支持重载
707浏览 • 1回复 待解决
关于文档中说har中不能用单例疑惑
382浏览 • 1回复 待解决