回复
     HarmonyOS/OpenHarmony应用开发-stage使用显示Want启动Ability
鸿蒙时代
 发布于 2023-2-7 10:17
 浏览
 0收藏
1.创建stage模型的项目

2.初始的entryability默认绑定index.ets

3.新建一个Ability与page,并且绑定



4.pages下的Index.ets代码:
import context from '@ohos.application.context';
@Entry
@Component
struct Index {
  @State message: string = '第一个Ability的page'
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button("CLICKME")
          .onClick(this.explicitStartAbility)
      }
      .width('100%')
    }
    .height('100%')
  }
  async explicitStartAbility() {
    try {
      let want = {
        deviceId: "",
        bundleName: "com.example.want_demo",
        abilityName: "EntryAbility1"
      };
      let context = getContext(this) as context.AbilityContext;
      await context.startAbility(want);
      console.info(`explicit start ability succeed`);
    } catch (err) {
      console.info(`explicit start ability failed with ` + err.code);
    }
  }
}
pages下的Page.ets代码:
@Entry
@Component
struct Page {
  @State message: string = '第二个Ability的page'
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}
5.打开模拟器浏览演示


标签 
 HarmonyOSOpenHarmony应用开发-stage使用显.docx 343.63K 7次下载  
已于2023-2-7 10:19:19修改
 
        赞
        
 
        收藏 
      
 回复
  相关推荐
 



















