> 文档中心 > HarmonyOS/OpenHarmony应用开发-stage使用显示Want启动Ability

HarmonyOS/OpenHarmony应用开发-stage使用显示Want启动Ability

1.创建stage模型的项目

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

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

4.pages下的Index.ets代码

import context from '@ohos.application.context';@Entry@Componentstruct 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@Componentstruct Page {  @State message: string = '第二个Ability的page'  build() {    Row() {      Column() { Text(this.message)   .fontSize(50)   .fontWeight(FontWeight.Bold)      }      .width('100%')    }    .height('100%')  }}

5.打开模拟器浏览演示