鸿蒙~UiAbility 组件间跳转

2024-01-01  本文已影响0人  胡修波

启动应用内的UIAbility :即UiAbility 组件间跳转、交互包括:(华为官网文档写的不够详细)

一、 同一个模块

import common from '@ohos.app.ability.common'


const TAG = "huxiubo"
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  private  context = getContext(this) as common.UIAbilityContext
  private  MyNumber: number
  private  MyNString: string;


  // hello = globalThis.hello
  eventHub() {
    this.context.eventHub.emit("event", 1, "huxiubo", "sulijing")

    this.MyNString = globalThis.MyNString
    this.MyNumber = globalThis.MyNumber
    console.info(TAG, `MyNString, ${ this.MyNString}`);
    console.info(TAG, `MyNumber, ${ this.MyNumber}`);
  }

  startOtherAbility() {
    console.info(TAG, `startOtherAbility`);
    let want = {
      deviceId: '', // deviceId为空表示本设备
      bundleName: 'com.example.myapplication',
      abilityName: 'TestAbility',
    }
    try {
      this.context.startAbility(want)
        .then(() => {
          console.info(TAG, `startAbility Success`);
        })
        .catch((err) => {
          console.info(TAG, `Failed: ${JSON.stringify(err)}}`);
        })
    } catch (error) {
      console.log("startAbility error: " + error)
    }
  }
  build() {
    Row() {
      Column() {
        Text("enter Ability")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.startOtherAbility()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}


二、不同模块

import common from '@ohos.app.ability.common'


const TAG = "huxiubo"
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  private  context = getContext(this) as common.UIAbilityContext
  private  MyNumber: number
  private  MyNString: string;


  // hello = globalThis.hello
  eventHub() {
    this.context.eventHub.emit("event", 1, "huxiubo", "sulijing")

    this.MyNString = globalThis.MyNString
    this.MyNumber = globalThis.MyNumber
    console.info(TAG, `MyNString, ${ this.MyNString}`);
    console.info(TAG, `MyNumber, ${ this.MyNumber}`);
  }

  startOtherAbility() {
    console.info(TAG, `startOtherAbility`);
    let want = {
      deviceId: '', // deviceId为空表示本设备
      bundleName: 'com.example.myapplication',
      abilityName: 'Module1Ability',
      moduleName: 'module1', // moduleName非必选
      // parameters: { // 自定义信息
      //   instanceKey: ge(),
      // },
    }
    try {
      this.context.startAbility(want)
        .then(() => {
          console.info(TAG, `startAbility Success`);
        })
        .catch((err) => {
          console.info(TAG, `Failed: ${JSON.stringify(err)}}`);
        })
    } catch (error) {
      console.log("startAbility error: " + error)
    }
  }
  build() {
    Row() {
      Column() {
        Text("enter Ability")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.startOtherAbility()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

注意 want,一定要写正确

 let want = {
      deviceId: '', // deviceId为空表示本设备
      bundleName: 'com.example.myapplication',
      abilityName: 'Module1Ability',
      moduleName: 'module1', // moduleName非必选
    }

编译运行代码,发现 {"code":16000001}}

上一篇 下一篇

猜你喜欢

热点阅读