or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

ai-ml-apis.mdapp-page-lifecycle.mdbluetooth-nfc-apis.mdcanvas-graphics.mdcloud-services.mdcomponent-system.mdcore-apis.mddevice-hardware-apis.mdevent-system.mdindex.mdpayment-apis.md

app-page-lifecycle.mddocs/

0

# App and Page Lifecycle

1

2

Complete type definitions for WeChat Mini Program app and page lifecycles, including launch options, scene values, and lifecycle methods.

3

4

## Capabilities

5

6

### App Constructor

7

8

Defines a WeChat Mini Program app with lifecycle methods and global data.

9

10

```typescript { .api }

11

/**

12

* Create a WeChat Mini Program app

13

* @param options - App configuration with lifecycle methods and global data

14

*/

15

function App<TData extends WechatMiniprogram.App.DataOption>(

16

options: WechatMiniprogram.App.Option<TData>

17

): void;

18

```

19

20

**Usage Examples:**

21

22

```typescript

23

App({

24

globalData: {

25

userInfo: null,

26

version: '1.0.0'

27

},

28

onLaunch(options) {

29

console.log('App launched with scene:', options.scene);

30

// Initialize app

31

},

32

onShow(options) {

33

console.log('App showed');

34

// App enters foreground

35

},

36

onHide() {

37

console.log('App hidden');

38

// App enters background

39

},

40

onError(error) {

41

console.error('App error:', error);

42

// Handle app errors

43

}

44

});

45

```

46

47

### App Instance Access

48

49

Get the current app instance to access global data and methods.

50

51

```typescript { .api }

52

/**

53

* Get current app instance

54

* @param options - Options for app access

55

* @returns App instance with global data and methods

56

*/

57

function getApp<T extends WechatMiniprogram.App.Instance>(

58

options?: WechatMiniprogram.App.GetAppOption

59

): T;

60

61

interface WechatMiniprogram.App.GetAppOption {

62

/** Allow access to app instance during app launch */

63

allowDefault?: boolean;

64

}

65

```

66

67

**Usage Examples:**

68

69

```typescript

70

// Access app from page or component

71

const app = getApp();

72

console.log('App version:', app.globalData.version);

73

app.globalData.userInfo = { name: 'John' };

74

75

// Safe access during launch

76

const app = getApp({ allowDefault: true });

77

```

78

79

### Page Constructor

80

81

Defines a WeChat Mini Program page with data, lifecycle methods, and event handlers.

82

83

```typescript { .api }

84

/**

85

* Create a WeChat Mini Program page

86

* @param options - Page configuration with data, lifecycle, and methods

87

*/

88

function Page<TData extends WechatMiniprogram.Page.DataOption>(

89

options: WechatMiniprogram.Page.Option<TData>

90

): void;

91

```

92

93

**Usage Examples:**

94

95

```typescript

96

Page({

97

data: {

98

items: [] as string[],

99

loading: false,

100

count: 0

101

},

102

onLoad(query) {

103

console.log('Page loaded with query:', query);

104

this.loadData();

105

},

106

onShow() {

107

console.log('Page showed');

108

},

109

onReady() {

110

console.log('Page ready - initial render complete');

111

},

112

onHide() {

113

console.log('Page hidden');

114

},

115

onUnload() {

116

console.log('Page unloaded');

117

},

118

onPullDownRefresh() {

119

this.loadData();

120

wx.stopPullDownRefresh();

121

},

122

loadData() {

123

this.setData({ loading: true });

124

// Load data logic

125

this.setData({ loading: false });

126

}

127

});

128

```

129

130

### Page Stack Access

131

132

Get the current page stack to access active pages.

133

134

```typescript { .api }

135

/**

136

* Get current pages stack

137

* @returns Array of current page instances

138

*/

139

function getCurrentPages<T extends WechatMiniprogram.Page.Instance[]>(): T;

140

```

141

142

**Usage Examples:**

143

144

```typescript

145

// Get current page stack

146

const pages = getCurrentPages();

147

const currentPage = pages[pages.length - 1];

148

console.log('Current page route:', currentPage.route);

149

150

// Access previous page

151

if (pages.length > 1) {

152

const prevPage = pages[pages.length - 2];

153

prevPage.setData({ returnValue: 'data from current page' });

154

}

155

```

156

157

## App Lifecycle Methods

158

159

```typescript { .api }

160

interface WechatMiniprogram.App.Option<TData> {

161

/** App launch - called once when app starts */

162

onLaunch?(options: WechatMiniprogram.App.LaunchShowOption): void;

163

164

/** App show - called when app enters foreground */

165

onShow?(options: WechatMiniprogram.App.LaunchShowOption): void;

166

167

/** App hide - called when app enters background */

168

onHide?(): void;

169

170

/** App error - called when app encounters error */

171

onError?(error: string): void;

172

173

/** Page not found - called when navigating to non-existent page */

174

onPageNotFound?(options: WechatMiniprogram.App.PageNotFoundOption): void;

175

176

/** Unhandled rejection - called for unhandled promise rejections */

177

onUnhandledRejection?(options: WechatMiniprogram.App.UnhandledRejectionOption): void;

178

179

/** Theme change - called when system theme changes */

180

onThemeChange?(options: WechatMiniprogram.App.ThemeChangeOption): void;

181

}

182

183

interface WechatMiniprogram.App.LaunchShowOption {

184

/** Scene value indicating how the app was launched */

185

scene: number;

186

/** Launch query parameters */

187

query: Record<string, string>;

188

/** Share ticket for group sharing */

189

shareTicket?: string;

190

/** Referrer info */

191

referrerInfo?: {

192

appId: string;

193

extraData?: Record<string, any>;

194

};

195

}

196

197

interface WechatMiniprogram.App.PageNotFoundOption {

198

/** Path of the page that was not found */

199

path: string;

200

/** Query parameters */

201

query: Record<string, string>;

202

/** Whether this is a tab bar page */

203

isEntryPage: boolean;

204

}

205

206

interface WechatMiniprogram.App.UnhandledRejectionOption {

207

/** Promise rejection reason */

208

reason: string;

209

/** Promise that was rejected */

210

promise: Promise<any>;

211

}

212

213

interface WechatMiniprogram.App.ThemeChangeOption {

214

/** Current theme: light or dark */

215

theme: 'light' | 'dark';

216

}

217

```

218

219

## Page Lifecycle Methods

220

221

```typescript { .api }

222

interface WechatMiniprogram.Page.Option<TData> {

223

/** Page data */

224

data?: TData;

225

226

/** Page load - called when page loads */

227

onLoad?(query: Record<string, string | undefined>): void;

228

229

/** Page show - called when page becomes visible */

230

onShow?(): void;

231

232

/** Page ready - called after initial render completes */

233

onReady?(): void;

234

235

/** Page hide - called when page becomes hidden */

236

onHide?(): void;

237

238

/** Page unload - called when page is destroyed */

239

onUnload?(): void;

240

241

/** Route done - called when route animation completes */

242

onRouteDone?(): void;

243

244

/** Pull down refresh - called when user pulls down to refresh */

245

onPullDownRefresh?(): void;

246

247

/** Reach bottom - called when page scrolls to bottom */

248

onReachBottom?(): void;

249

250

/** Page scroll - called when page scrolls */

251

onPageScroll?(options: WechatMiniprogram.Page.PageScrollOption): void;

252

253

/** Tab item tap - called when tab bar item is tapped */

254

onTabItemTap?(options: WechatMiniprogram.Page.TabItemTapOption): void;

255

256

/** Window resize - called when window size changes */

257

onResize?(options: WechatMiniprogram.Page.ResizeOption): void;

258

259

/** Share app message - called when sharing to chat */

260

onShareAppMessage?(

261

options: WechatMiniprogram.Page.ShareAppMessageOption

262

): WechatMiniprogram.Page.CustomShareContent;

263

264

/** Share timeline - called when sharing to WeChat Moments */

265

onShareTimeline?(): WechatMiniprogram.Page.ShareTimelineContent;

266

267

/** Add to favorites - called when adding to favorites */

268

onAddToFavorites?(

269

options: WechatMiniprogram.Page.AddToFavoritesOption

270

): WechatMiniprogram.Page.AddToFavoritesContent;

271

272

/** Save exit state - called when page needs to save state */

273

onSaveExitState?(): WechatMiniprogram.Page.ExitState;

274

}

275

276

interface WechatMiniprogram.Page.PageScrollOption {

277

/** Vertical scroll position in px */

278

scrollTop: number;

279

}

280

281

interface WechatMiniprogram.Page.ShareAppMessageOption {

282

/** Share trigger: button, menu */

283

from: 'button' | 'menu';

284

/** Target element for button shares */

285

target?: any;

286

/** Web page URL for sharing */

287

webViewUrl?: string;

288

}

289

```

290

291

## Page Instance Methods

292

293

```typescript { .api }

294

interface WechatMiniprogram.Page.Instance<TData> {

295

/** Current page route */

296

readonly route: string;

297

298

/** Page load options */

299

readonly options: Record<string, string>;

300

301

/** Update page data and trigger re-render */

302

setData(

303

data: Partial<TData>,

304

callback?: () => void

305

): void;

306

}

307

```

308

309

## Scene Values

310

311

```typescript { .api }

312

// Common scene values for app launch

313

enum WechatMiniprogram.App.SceneValues {

314

/** Launch from chat session discovery */

315

GROUP_CHAT_DISCOVERY = 1001,

316

317

/** Launch from group chat */

318

GROUP_CHAT = 1005,

319

320

/** Launch from one-on-one chat */

321

SINGLE_CHAT = 1006,

322

323

/** Launch from WeChat scan */

324

SCAN_QR_CODE = 1011,

325

326

/** Launch from long press to identify QR code */

327

LONG_PRESS_QR_CODE = 1012,

328

329

/** Launch from app search */

330

SEARCH = 1017,

331

332

/** Launch from WeChat Moments */

333

MOMENTS = 1020,

334

335

/** Launch from nearby mini programs */

336

NEARBY = 1023,

337

338

/** Launch from profile page */

339

PROFILE = 1024,

340

341

/** Launch from system search */

342

SYSTEM_SEARCH = 1025,

343

344

/** Launch from tab bar */

345

TAB_BAR = 1089,

346

347

/** Launch from menu */

348

MENU = 1090

349

}

350

```

351

352

## Types

353

354

```typescript { .api }

355

namespace WechatMiniprogram.App {

356

interface Instance<TData = any> {

357

/** Global data shared across the app */

358

globalData: TData;

359

}

360

361

interface DataOption {

362

[key: string]: any;

363

}

364

365

interface Option<TData> extends Instance<TData> {

366

[key: string]: any;

367

}

368

}

369

370

namespace WechatMiniprogram.Page {

371

interface Instance<TData = any> {

372

readonly route: string;

373

readonly options: Record<string, string>;

374

setData(data: Partial<TData>, callback?: () => void): void;

375

}

376

377

interface DataOption {

378

[key: string]: any;

379

}

380

381

interface Option<TData> {

382

data?: TData;

383

[key: string]: any;

384

}

385

}

386

```