启动应用内的UIAbility :即UiAbility 组件间跳转、交互包括:(华为官网文档写的不够详细)
一、 同一个模块
-
新建一个UIAbility:TestAbility.ts
-
在 modlue.json5中 abilities中添加TestAbility
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
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,添加模块