• 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 **commonEventManager** module.
19
20   ```ts
21   import commonEventManager from '@ohos.commonEventManager';
22   import Base from '@ohos.base';
23   ```
24
252. Subscribe to an event by following the procedure described in [Subscribing to Common Events in Dynamic Mode](common-event-subscription.md).
26
273. Call **unsubscribe** in **CommonEvent** to unsubscribe from the common event.
28
29   ```ts
30   // The subscriber object is created during event subscription.
31   if (subscriber !== null) {
32       commonEventManager.unsubscribe(subscriber, (err: Base.BusinessError) => {
33           if (err) {
34               console.error(`[CommonEvent] UnsubscribeCallBack err=${JSON.stringify(err)}`);
35           } else {
36               console.info(`[CommonEvent] Unsubscribe`);
37               subscriber = null;
38           }
39       })
40   }
41   ```
42