您的位置:首页 > 房产 > 家装 > 鸿蒙卡片传值app到卡片

鸿蒙卡片传值app到卡片

2024/10/5 20:22:36 来源:https://blog.csdn.net/jian11058/article/details/141469463  浏览:    关键词:鸿蒙卡片传值app到卡片

WidgetCard.ets
@Entry
@Component
struct WidgetCard {@State num: number = 0@LocalStorageProp('formId')@Watch('updataFormId')formId:string=''updataFormId(){// 卡片向app传参postCardAction(this, {action: 'call',abilityName: 'EntryAbility',params:{method:'updataFormId',num:this.formId}});}build() {Row() {Column() {Text('ID:'+this.formId)Button(this.num + '++').onClick(() => {this.num++postCardAction(this, {action: 'call',abilityName: 'EntryAbility',params:{method:'updataNum',num:this.num}});})}.width('100%')}.height('100%').onClick(() => {postCardAction(this, {action: 'router', //跳转事件abilityName: 'EntryAbility',});})}
}

获取卡片id

在卡片的entryability中:EntryFormAbility

export default class EntryFormAbility extends FormExtensionAbility {onAddForm(want: Want) {// Called to return a FormBindingData object.let formData = '';return formBindingData.createFormBindingData({// form idformId:want.parameters!['ohos.extra.param.key.form_identity']as string});}

在app的entryability中:


class Params implements rpc.Parcelable{marshalling(dataOut: rpc.MessageSequence): boolean {return true}unmarshalling(dataIn: rpc.MessageSequence): boolean {return true}}class CaraParam{num:number=0formId:string=''
}export default class EntryAbility extends UIAbility {onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');this.callee.on('updataNum',(data)=>{let res=JSON.parse(data.readString())as CaraParamAppStorage.setOrCreate('num',res.num)return new Params()})// 卡片创建时,调用了updataFormId,此时这里就会被触发,然后app将值传过去this.callee.on('updataFormId',(data)=>{let res=JSON.parse(data.readString())as CaraParam// AppStorage.setOrCreate('num',res.formId)// 数据可以写到首选项中存储// 把最新的数据推过去formProvider.updateForm(res.formId,formBindingData.createFormBindingData({num:AppStorage.get('num')}))return new Params()})}

版权声明:

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

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