Introduction to the JavaScript APIs available for Mini Program development.
The framework provides developers with JSAPI and OpenAPI capabilities to launch diversified convenient services to users. This section covers the client-side JavaScript APIs (JSAPI) that Mini Apps can use to interact with the platform and device features.
APIs prefixed with my.on listen to system events and accept callback functions. When the event is triggered, the callback is executed.
Copy
// Register event listenermy.onAppShow((res) => { console.log('App became visible', res);});// Unregister specific listenermy.offAppShow(callback);// Unregister all listeners for an eventmy.offAppShow();
Always clean up event listeners when they’re no longer needed, such as in page onUnload lifecycle hooks, to prevent memory leaks.
Before using an API, you can check if it’s supported in the current environment:
Copy
if (my.canIUse('getLocation')) { my.getLocation({ success: (res) => { console.log(res); } });} else { console.log('getLocation is not supported');}
Always use feature detection for APIs that may not be available on all platform versions. This ensures your Mini App degrades gracefully on older versions.