• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Unsubscribing from Common Events in Dynamic Mode
2
3
4## When to Use
5
6You can call [unsubscribe()](../reference/apis/js-apis-commonEventManager.md#commoneventmanagerunsubscribe) to unsubscribe from a common event that is no longer required in dynamic mode.
7
8
9## Available APIs
10
11| API| Description|
12| -------- | -------- |
13| unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback) | Unsubscribes from a common event.|
14
15
16## How to Develop
17
181. Import the **commonEvent** module.
19
20   ```ts
21   import commonEvent from '@ohos.commonEventManager';
22   ```
23
242. Subscribe to an event by following the procedure described in [Subscribing to Common Events in Dynamic Mode](common-event-subscription.md).
25
263. Call **unsubscribe** in **CommonEvent** to unsubscribe from the common event.
27
28   ```ts
29   // The subscriber object is created during event subscription.
30   if (subscriber !== null) {
31       commonEvent.unsubscribe(subscriber, (err) => {
32           if (err) {
33               console.error(`[CommonEvent] UnsubscribeCallBack err=${JSON.stringify(err)}`)
34           } else {
35               console.info(`[CommonEvent] Unsubscribe`)
36               subscriber = null
37           }
38       })
39   }
40   ```
41