---
title: App
---

# App(config: Object)

## Description

Register a miniapp. An Object parameter is used to specify the lifecycle callback of the miniapp.

**`App()` must be called in the `app.js` file only once. Otherwise, unexpected consequences might occur.**

## Parameter

| Property                                        | Type     | Default value | Required | Description                                                                                                     | Minimum version |
| ----------------------------------------------- | -------- | ------------- | -------- | --------------------------------------------------------------------------------------------------------------- | --------------- |
| [onLaunch](#onlaunchoptions-object)             | function |               | No       | The lifecycle callback to invoke when the miniapp is initialized.                                               |
| [onShow](#onshowoptions-object)                 | function |               | No       | The lifecycle callback to invoke when the miniapp is started or switches to the foreground.                     |
| [onHide](#onhide)                               | function |               | No       | The lifecycle callback to invoke when the miniapp switches to the background.                                   |
| [onError](#onerrorstring-error)                 | function |               | No       | The listener for errors.                                                                                        |
| [onPageNotFound](#onpagenotfoundoptions-object) | function |               | No       | The listener for a page that does not exist.                                                                    |
| [onThemeChange](#onthemechangeoptions-object)   | function |               | No       | The listener for changes in the system theme.                                                                   |
| [onEvent](#oneventoptions-object)               | function |               | No       | The event that is triggered when users tap the dashboard.                                                       |
| Others                                          | any      |               | No       | You can add any function or data variable to `Object` and access the function or data variable by using `this`. |

## Lifecycle

### onLaunch(options: Object)

Triggered once only globally when the miniapp is initialized. You can also use [ty.getLaunchOptionsSync](/en/miniapp/develop/miniapp/api/base/container/getLaunchOptionsSync) to get parameters.

**Parameter**: the same as [ty.getLaunchOptionsSync](/en/miniapp/develop/miniapp/api/base/container/getLaunchOptionsSync).

### onShow(options: Object)

Triggered when the miniapp is started or switches from the background to the foreground. You can also use [ty.onAppShow](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onAppShow) to bind the miniapp with the listener.

**Parameter**: the same as [ty.onAppShow](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onAppShow).

### onHide()

Triggered when the miniapp switches from the foreground to the background. You can also use [ty.onAppHide](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onAppHide) to bind the miniapp with the listener.

### onError(String error)

Triggered when a script error or an API error occurs. You can also use [ty.onError](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onError) to bind the miniapp with the listener.

**Parameter**: the same as [ty.onError](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onError).

### onPageNotFound(options: Object)

Triggered when the specified miniapp page does not exist. You can also use [ty.onPageNotFound](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onPageNotFound) to bind the miniapp with the listener.
**Parameter**: the same as [ty.onPageNotFound](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onPageNotFound).

Example:

```js
App({
  onPageNotFound(res) {
    ty.redirectTo({
      url: '/pages/...',
    }); // For a tab bar page, use `ty.switchTab`.
  },
});
```

### onThemeChange(options: Object)

Triggered when the system switches between themes. You can also use [ty.onThemeChange](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onThemeChange) to bind the miniapp with the listener.

**Parameter**: the same as [ty.onThemeChange](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onThemeChange).

## Example

```javascript
App({
  onLaunch(options) {
    // Do something initial when launch.
  },
  onShow(options) {
    // Do something when show.
  },
  onHide() {
    // Do something when hide.
  },
  onError(msg) {
    console.log(msg);
  },
  globalData: 'I am global data',
});
```

### onEvent(options: Object)

Triggered when users tap the dashboard. The parameter is the object in `boardMenus`.

**Parameter**: the same as [ty.onAppEvent](/en/miniapp/develop/miniapp/api/base/applicationLevelEvents/onAppEvent).

## Example

```javascript
App({
  onEvent(option) {
    console.log(option);
  },
  globalData: 'I am global data',
});
```
