---
title: Communication Between Miniapps
---

## Background information

Some cohesive, specific functional modules may be split into multiple MiniApps. These MiniApps communicate and work together to complete data transfer and status synchronization.

> The base library version is v2.9.0 or later.

## Open other MiniApps

You can call `ty.navigateToMiniProgram` to enable navigation from a miniapp to other MiniApps. The parameters are as follows:

| Parameter | Type | Required | Description |
| ---------- | -------- | ---- | ------------------ |
| appId | string | Yes | The ID of a specified miniapp to be opened. |
| position | string | No | The location of a specified miniapp to be opened. Valid values: `right` and `bottom`. Default value: `bottom`. |
| path | string | No | The page path. If the value is empty, the homepage will be opened. The part following the question mark (`?`) in the `path` will become the `query`, which is the callback function of `App.onLaunch`, `App.onShow`, and `Page.onLoad` of the miniapp. |
| extraData | object | No | The data that is passed to the target miniapp. The target miniapp can get the data in `App.onLaunch` and `App.onShow`. |
| envVersion | string | No | The version of a specified miniapp to be opened. This parameter is valid only when the current miniapp is the development version or the trial version. If the current miniapp is the official version, the miniapp to be opened must be the official version. |
| shortLink | string | No | The URL of the miniapp. After this parameter is passed, the miniapp ID and `path` can be omitted. |
| events | object | No | The communication events between MiniApps. For more information, see the next item. |
| complete | function | No | The callback to invoke when the API call ends. This callback is executed regardless of whether the call was successful or not. |
| success | function | No | The success callback. |
| fail | function | No | The fail callback. |

Events can be passed using the `events` parameter. You can listen for or call the target miniapp page using `this.getOpenerAppChannel` to achieve communication between the two MiniApps.

When `events` are declared, the return result of `success(result)` contains the `result.appChannel` object. Events can be sent to the target miniapp through the `appChannel` object.

## Example

Navigate from miniapp A to miniapp B, and achieve communications between them:

```js
// Miniapp A
Page({
  // Tap the button to open miniapp B
  handleOpen() {
    ty.navigateToMiniProgram({
      appId: 'The ID of miniapp B',
      events: {
        // Provide events that can be requested by the target miniapp
        acceptDataByChild: (data) => {
          console.log('Data from miniapp B:', data);
        },
      },
      success(res) {
        // Miniapp B is opened
        // Send events to miniapp B through res.appChannel
        res.appChannel.emit('emitDataToChild', { data: 'hello' });
      },
    });
  },
});
```

Page of miniapp B

```js
// Default homepage of miniapp B
Page({
  onLoad() {
    const appChannel = this.getOpenerAppChannel();

    // Listen for the event call of miniapp A
    appChannel.on('emitDataToChild', (data) => {
      console.log('Data from miniapp A:', data);
    });

    // Call the event declared in events when miniapp A calls navigateToMiniProgram
    appChannel.emit('acceptDataByChild', { data: 'world' });
  },
});
```
