@@ -25,13 +25,13 @@ You can create a subscriber object to subscribe to a common event to obtain the
...
@@ -25,13 +25,13 @@ You can create a subscriber object to subscribe to a common event to obtain the
### How to Develop
### How to Develop
1. Import the **commonEvent** module.
1. Import the **commonEvent** module.
```javascript
```js
importcommonEventfrom'@ohos.commonEvent';
importcommonEventfrom'@ohos.commonEvent';
```
```
2. Create a **subscribeInfo** object. For details about the data types and parameters of the object, see [CommonEventSubscribeInfo](../reference/apis/js-apis-commonEvent.md#commoneventsubscribeinfo).
2. Create a **subscribeInfo** object. For details about the data types and parameters of the object, see [CommonEventSubscribeInfo](../reference/apis/js-apis-commonEvent.md#commoneventsubscribeinfo).
```javascript
```js
privatesubscriber=null// Used to save the created subscriber object for subsequent subscription and unsubscription.
privatesubscriber=null// Used to save the created subscriber object for subsequent subscription and unsubscription.
// Subscriber information
// Subscriber information
...
@@ -42,7 +42,7 @@ var subscribeInfo = {
...
@@ -42,7 +42,7 @@ var subscribeInfo = {
3. Create a subscriber object and save the returned object for subsequent operations such as subscription and unsubscription.
3. Create a subscriber object and save the returned object for subsequent operations such as subscription and unsubscription.
4. Create a subscription callback, which is triggered when an event is received. The data returned by the subscription callback contains information such as the common event name and data carried by the publisher. For details about the data types and parameters of the common event data, see [CommonEventData](../reference/apis/js-apis-commonEvent.md#commoneventdata).
4. Create a subscription callback, which is triggered when an event is received. The data returned by the subscription callback contains information such as the common event name and data carried by the publisher. For details about the data types and parameters of the common event data, see [CommonEventData](../reference/apis/js-apis-commonEvent.md#commoneventdata).
#### Development for Publishing a Common Event with Given Attributes
#### Development for Publishing a Common Event with Given Attributes
1. Import the **commonEvent** module.
1. Import the **commonEvent** module.
```javascript
```js
importcommonEventfrom'@ohos.commonEvent'
importcommonEventfrom'@ohos.commonEvent'
```
```
2. Define attributes of the common event to publish. For details about the data types and parameters in the data to publish, see [CommonEventPublishData](../reference/apis/js-apis-commonEvent.md#commoneventpublishdata).
2. Define attributes of the common event to publish. For details about the data types and parameters in the data to publish, see [CommonEventPublishData](../reference/apis/js-apis-commonEvent.md#commoneventpublishdata).
```javascript
```js
// Attributes of a common event.
// Attributes of a common event.
varoptions={
varoptions={
code:1,// Result code of the common event
code:1,// Result code of the common event
...
@@ -125,7 +125,7 @@ var options = {
...
@@ -125,7 +125,7 @@ var options = {
3. Pass in the common event name, attributes of the common event, and callback, and publish the event.
3. Pass in the common event name, attributes of the common event, and callback, and publish the event.
```javascript
```js
// Publish a common event.
// Publish a common event.
commonEvent.publish("event",options,(err)=>{
commonEvent.publish("event",options,(err)=>{
if(err.code){
if(err.code){
...
@@ -149,14 +149,14 @@ You can use the **unsubscribe** API to unsubscribe from a common event.
...
@@ -149,14 +149,14 @@ You can use the **unsubscribe** API to unsubscribe from a common event.
### How to Develop
### How to Develop
1. Import the **commonEvent** module.
1. Import the **commonEvent** module.
```javascript
```js
importcommonEventfrom'@ohos.commonEvent';
importcommonEventfrom'@ohos.commonEvent';
```
```
2. Subscribe to a common event by following instructions in [Common Event Subscription Development](#Common-Event-Subscription-Development).
2. Subscribe to a common event by following instructions in [Common Event Subscription Development](#Common-Event-Subscription-Development).
3. Invoke the **unsubscribe** API in **CommonEvent** to unsubscribe from the common event.
3. Invoke the **unsubscribe** API in **CommonEvent** to unsubscribe from the common event.