您的位置:首页 > 新闻 > 资讯 > 淘宝官网首页_在线定制网站官网_如何自己制作一个网站_百度在线识图查图片

淘宝官网首页_在线定制网站官网_如何自己制作一个网站_百度在线识图查图片

2024/10/9 3:08:31 来源:https://blog.csdn.net/Crowd_chips/article/details/142642777  浏览:    关键词:淘宝官网首页_在线定制网站官网_如何自己制作一个网站_百度在线识图查图片
淘宝官网首页_在线定制网站官网_如何自己制作一个网站_百度在线识图查图片

标题

文章目录

  • 一、什么是Ability?
  • 二、使用步骤(单例和多例)
  • 三、窗口
  • 四、通知


一、什么是Ability?

开发模式提供的开发功能抽象的描述。
其中重要的是UiAbility,界面组件能力,负责所有界面的处理。
通过配置可以变更单例,多例,指定实例,module.json5中进行配置
如:
单例:lunchType:'singleton'
多例:lunchType:'standard'
指定实例:lunchType:'specified'

二、使用步骤(单例和多例)

当一个项目有多个ability时,则需要进行配置,才可以启动子模块
在这里插入图片描述

1.单例和多例

操作步骤:
1.module.json5中进行配置单例、多例、指定实例。
单例:lunchType:'singleton'
多例:lunchType:'standard'"abilities": [{"launchType": "singleton"}]
2.唤起页面://2.1 链接上下文private cont = getContext(this) as common.UIAbilityContext//2.2 定义参数Wantconst want: Want = {// deviceId:'', 标识ID,可选bundleName: 'com.www.myapplication', //包名: appScopemoduleName: 'entry', // 模块名称 module.json5abilityName: 'EntryAbilityNext', //应用名称  module.json5parameters: {} // 参数}//2.3 拉起页面con.startAbility(want)3.若传递了参数,则需要在拉起页面中的ability中进行接收onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {this.getParams(want)}onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {this.getParams(want)}getParams(want: Want) {const par = want.parameters as Record<string, string>AppStorage.setOrCreate('message', par.message)}

2.指定实例

操作步骤:
思路:
// 子模块配置指定实例
// 链接上下文
// 配置Want
// 调用函数并传参,结束接收数据
// 创建MyAbilityStage , 并重写函数onAcceptWant
// 配置module.json 定义路径
// 在子模块UiAbility接收参数(onCreat和onNewWant)1.module.json5中进行配置单例、多例、指定实例。"abilities": [指定实例:lunchType:'specified'{"launchType": "specified"}]
2.唤起页面://2.1 链接上下文private cont = getContext(this) as common.UIAbilityContext//2.2 定义参数Want//2.3 拉起页面this.cont.startAbilityForResult({ bundleName: 'com.www.myapplication', //包名: appScopemoduleName: 'OtherApplication',  // 模块名称 module.json5abilityName: 'OtherApplicationAbility',  // 模块名称 module.json5parameters: {message: '传递子给的模块数据'  // 参数key:id // 用于指定实例}}).then((res) => {// 返回的数据const data = res.want?.parameters as Record<string, string>console.log('system===>回调接收数据' + JSON.stringify(data.message))this.message = data.message}).catch((err: BusinessError) => {promptAction.showToast({ message: '开启实例异常' + JSON.stringify(err) })})3.若传递了参数,则需要在拉起页面中的ability中进行接收onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {this.getParams(want)}onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {this.getParams(want)}getParams(want: Want) {const par = want.parameters as Record<string, string>AppStorage.setOrCreate('message', par.message)}
4. 在拉起页面创建MyAbilityStage类export default class MyAbilityStage extends AbilityStage {// 重写函数override onAcceptWant(want: Want) {if (want.abilityName === 'InnerEntryAbility') {console.log('system===>重写' + want.abilityName)const param = want.parameters as Record<string, string>return 'InnerEntryAbility' + param.key}return ''}
}
5.module.json5中配置,见下面"srcEntry": "./ets/abilityStage/MyAbilityStage.ets",6.在拉起页面编辑往主模块返回数据
// 链接上下文const cont = getContext(this) as common.UIAbilityContext// want :拉起主应用// 调用函数,返回结果并可以关闭当前应用cont.terminateSelfWithResult({ // 返回参数使用的resultCode: 200, want: {bundleName: 'com.www.myapplication',moduleName: 'entry',abilityName: 'EntryAbility',parameters: {message: '子模块数据处理完成'}}}, () => {// 关闭应用})

在这里插入图片描述

三、窗口

思路:
1.ability中初始化生命周期将windowStage保存到全局应用中。
2.在需要开启子窗口的位置,设置大小,位置,加载页面遮罩层等UI,并接收window结果值保存到全局应用中
3.在小窗口位置调用window,执行关闭并操作其他业务(关闭主窗口的遮罩层,则需要进行使用订阅)

1.ability中初始化生命周期将windowStage保存到全局应用中。

onWindowStageCreate(windowStage: window.WindowStage): void {AppStorage.setOrCreate('windowStage',windowStage)
}

2.在需要开启子窗口的位置,设置大小,位置,加载页面遮罩层等UI,并接收window结果值保存到全局应用中

1.变量// 读取全局应用状态private windowStage: window.WindowStage | undefined = AppStorage.get('windowStage')// 遮罩层@State isShow: boolean = false2.遮罩层UI
if (this.isShow) {Rect().width('100%').height('100%').backgroundColor("#ffddecec").opacity(0.2)}3.设置子窗口大小,位置:
// 弹出小窗口 .并将数据保存到应用状态管理中
this.windowStage?.createSubWindow('dLog').then((win: window.Window) => {const system = display.getDefaultDisplaySync() // 获取系统的数据,获取到宽高// 画小弹框的大小win.resize(system.width * 0.8, system.height * 0.2)// 所在位置win.moveWindowTo(system.width / 9, system.height - (system.height / 4))this.isShow = true//设置加载路径win.setUIContent('pages/CodePage', () => { // 需要重新创建一个entry//开启弹框win.showWindow().then(() => {})})// 保存应用AppStorage.setOrCreate('window', win)})

3.在小窗口位置调用window,执行关闭并操作其他业务(关闭主窗口的遮罩层,则需要进行使用订阅)

  private win: window.Window | undefined = AppStorage.get('window')//关闭窗口并关闭主窗口遮罩层(小窗口发布)this.win?.destroyWindow(()=>{getContext(this).eventHub.emit('close') // 点击取消或者确定使用订阅关闭遮罩层router.replaceUrl({url:'pages/HomePage'}) // 只有在会调用才可以进行跳转,同步操作})// 主窗口进行订阅,并执行相关操作// 生命周期加载订阅aboutToAppear(): void {getContext(this).eventHub.on('close', () => {this.isShow = false // 关闭遮罩层})}

四、通知

  // 开启通知,在组件加载的时候async getNotification() {let context = getContext(this) as common.UIAbilityContext;let result = await notificationManager.isNotificationEnabled()if (!result) {notificationManager.requestEnableNotification(context)}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com