1# @ohos.application.Want (Want) 2 3Want是对象间信息传递的载体, 可以用于应用组件间的信息传递。 Want的使用场景之一是作为startAbility的参数, 其包含了指定的启动目标, 以及启动时需携带的相关数据, 如bundleName和abilityName字段分别指明目标Ability所在应用Bundle名称以及对应包内的Ability名称。当Ability A需要启动Ability B并传入一些数据时, 可使用Want作为载体将这些数据传递给Ability B。 4 5> **说明:** 6> 7> 本模块首批接口从API version 8 开始支持,从API version 9废弃,替换模块为[@ohos.app.ability.Want](js-apis-app-ability-want.md)。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```ts 12import Want from '@ohos.application.Want'; 13``` 14 15## 属性 16 17**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityBase 18 19| 名称 | 类型 | 必填 | 说明 | 20| ----------- | -------------------- | ---- | ------------------------------------------------------------ | 21| deviceId | string | 否 | 表示运行指定Ability的设备ID。如果未设置该字段,则表明指定本设备。 | 22| bundleName | string | 否 | 表示Bundle名称。 | 23| abilityName | string | 否 | 表示待启动的Ability名称。如果在Want中该字段同时指定了BundleName和AbilityName,则Want可以直接匹配到指定的Ability。AbilityName需要在一个应用的范围内保证唯一。 | 24| uri | string | 否 | 表示Uri描述。如果在Want中指定了Uri,则Want将匹配指定的Uri信息,包括scheme, schemeSpecificPart, authority和path信息。 | 25| type | string | 否 | 表示MIME type类型描述,打开文件的类型,主要用于文管打开文件。比如:'text/xml' 、 'image/*'等,MIME定义参考:https://www.iana.org/assignments/media-types/media-types.xhtml?utm_source=ld246.com。 | 26| flags | number | 否 | 表示处理Want的方式。默认传数字,具体参考:[flags说明](js-apis-ability-wantConstant.md#wantconstantflags)。 | 27| action | string | 否 | 表示要执行的通用操作(如:查看、分享、应用详情)。在隐式Want中,您可以定义该字段,配合uri或parameters来表示对数据要执行的操作。具体参考:[action说明](js-apis-ability-wantConstant.md#wantconstantaction)。隐式Want定义及匹配规则参考:[显式Want与隐式Want匹配规则](../../application-models/explicit-implicit-want-mappings.md)。 | 28| parameters | {[key: string]: any} | 否 | 表示WantParams描述,由开发者自行决定传入的键值对。默认会携带以下key值:<br>ohos.aafwk.callerPid 表示拉起方的pid。<br>ohos.aafwk.param.callerToken 表示拉起方的token。<br>ohos.aafwk.param.callerUid 表示[bundleInfo](js-apis-bundle-BundleInfo.md#bundleinfo)中的uid,应用包里应用程序的uid。<br />- component.startup.newRules:表示是否启用新的管控规则。<br />- moduleName:表示拉起方的模块名,该字段的值即使定义成其他字符串,在传递到另一端时会被修改为正确的值。<br />- ohos.dlp.params.sandbox:表示dlp文件才会有。 | 29| entities | Array\<string> | 否 | 表示目标Ability额外的类别信息(如:浏览器、视频播放器)。在隐式Want中是对action字段的补充。在隐式Want中,您可以定义该字段,来过滤匹配Ability类型。具体参考:[entity说明](js-apis-ability-wantConstant.md#wantconstantentity)。 | 30 31**示例:** 32 33- 基础用法(在UIAbility对象中调用,其中示例中的context为UIAbility的上下文对象) 34 35 ```ts 36 import Want from '@ohos.application.Want'; 37 import { BusinessError } from '@ohos.base'; 38 39 let want: Want = { 40 'deviceId': '', // deviceId为空表示本设备 41 'bundleName': 'com.example.myapplication', 42 'abilityName': 'EntryAbility', 43 }; 44 this.context.startAbility(want, (error: BusinessError) => { 45 // 显式拉起Ability,通过bundleName、abilityName和moduleName可以唯一确定一个Ability 46 console.error('error.code = ${error.code}'); 47 }); 48 ``` 49 50- 通过自定字段传递数据, 以下为当前支持类型。(在UIAbility对象中调用,其中示例中的context为UIAbility的上下文对象) 51 52 * 字符串(String) 53 ```ts 54 import Want from '@ohos.application.Want'; 55 56 let want: Want = { 57 bundleName: 'com.example.myapplication', 58 abilityName: 'EntryAbility', 59 parameters: { 60 keyForString: 'str', 61 }, 62 }; 63 ``` 64 * 数字(Number) 65 ```ts 66 import Want from '@ohos.application.Want'; 67 68 let want: Want = { 69 bundleName: 'com.example.myapplication', 70 abilityName: 'EntryAbility', 71 parameters: { 72 keyForInt: 100, 73 keyForDouble: 99.99, 74 }, 75 }; 76 ``` 77 * 布尔(Boolean) 78 ```ts 79 import Want from '@ohos.application.Want'; 80 81 let want: Want = { 82 bundleName: 'com.example.myapplication', 83 abilityName: 'EntryAbility', 84 parameters: { 85 keyForBool: true, 86 }, 87 }; 88 ``` 89 * 对象(Object) 90 ```ts 91 import Want from '@ohos.application.Want'; 92 93 let want: Want = { 94 bundleName: 'com.example.myapplication', 95 abilityName: 'EntryAbility', 96 parameters: { 97 keyForObject: { 98 keyForObjectString: 'str', 99 keyForObjectInt: -200, 100 keyForObjectDouble: 35.5, 101 keyForObjectBool: false, 102 }, 103 }, 104 }; 105 ``` 106 * 数组(Array) 107 ```ts 108 import Want from '@ohos.application.Want'; 109 110 let want: Want = { 111 bundleName: 'com.example.myapplication', 112 abilityName: 'EntryAbility', 113 parameters: { 114 keyForArrayString: ['str1', 'str2', 'str3'], 115 keyForArrayInt: [100, 200, 300, 400], 116 keyForArrayDouble: [0.1, 0.2], 117 keyForArrayObject: [{obj1: 'aaa'}, {obj2: 100}], 118 }, 119 }; 120 ``` 121 * 文件描述符(FD) 122 ```ts 123 import fs from '@ohos.file.fs'; 124 import Want from '@ohos.application.Want'; 125 import { BusinessError } from '@ohos.base'; 126 127 let fd: number = 0; 128 try { 129 fd = fs.openSync('/data/storage/el2/base/haps/pic.png').fd; 130 } catch(e) { 131 console.error(`openSync fail: ${JSON.stringify(e)}`); 132 } 133 let want: Want = { 134 deviceId: '', // deviceId为空表示本设备 135 bundleName: 'com.example.myapplication', 136 abilityName: 'EntryAbility', 137 parameters: { 138 'keyFd':{'type':'FD', 'value':fd} 139 } 140 }; 141 this.context.startAbility(want, (error: BusinessError) => { 142 // 显式拉起Ability,通过bundleName、abilityName和moduleName可以唯一确定一个Ability 143 console.error('error.code = ${error.code}'); 144 }); 145 ``` 146 147- 更多详细说明和示例请参见: [应用模型](../../application-models/application-model-composition.md)的信息传递载体Want 148 149 <!--no_check-->