美文网首页
鸿蒙~UiAbility 组件间跳转

鸿蒙~UiAbility 组件间跳转

作者: 胡修波 | 来源:发表于2024-01-01 14:32 被阅读0次

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

一、 同一个模块

  • 新建一个UIAbility:TestAbility.ts


    微信图片_20240102140422.png
  • 在 modlue.json5中 abilities中添加TestAbility


    微信图片_20240102140635.png
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%')
  }
}


二、不同模块

  • 1、新建一个模块:右击项目 new->module , 填入 模块名称 modele1


    微信图片_20240102142511.png
  • 2、跳转代码

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

  • 3 错误代码:1600001,表示未找到,困扰好久,解决办法如下
    菜单栏 Run->edit configureation, 选择entry,Deploy Multi Hap,选中 Deploy Multil Hap Packages,添加模块


    微信图片_20240102143224.png

相关文章

  • Android组件化架构 - 3. 组件间跳转 & ARoute

    组件间跳转 & ARouter路由 常见的跳转,对于用户来说就是页面跳转,对应Android中就是activity...

  • iOS开发中组件化之业务组件间通讯方式之Target-Actio

    既然是组件间通讯,这里所说的组件就是业务组件。 跳转 实际就是控制器A跳转到控制器B,控制器B作为某个组件的一个入...

  • 路由模块

    路由模块控制vue内容显示,路由跳转,组件间的跳转。就是一个页面中实现不同组件内容的切换 1. 安装路由模块 ...

  • Android 组件化实践

    组件化MVP+Retrofit+Rxjava,项目使用kotlin,组件间跳转使用Arouter。 首先在项目下新...

  • Vue实战3-路由

    组件间跳转 在上次实现了我们的第一个页面之后,我们现在用router实现页面间的跳转。参考基于 Webpack &...

  • Android组件化

    1、本文主要内容 什么是组件化 组件化要解决的问题 组件的单独调试及集成 组件间通信 组件界面跳转 主项目获取并显...

  • ionic4-传递参数

    环境 路由传值 方式一(单个值) 路由定义 跳转 或者 接收 方式二(多个值) 跳转 接收 组件间传值 父 -> ...

  • iOS组件化组件间跳转通讯入门

    常见的有三种:1、URL Scheme2、CTMediator(Target-Action)3、Protocol-...

  • Arouter

    Arouter Arouter是一款路由框架,在做组件化是用于组件间通信(包括页面跳转,调用另一个组件的服务等),...

  • vue 路由传递参数

    父组件: 跳转路由子组件:

网友评论

      本文标题:鸿蒙~UiAbility 组件间跳转

      本文链接:https://www.haomeiwen.com/subject/ugeundtx.html