Get the App
SLTechnology News&Howtos  ›  Development  › 

What is WeChat Mini Programs's life cycle function?

Shulou Source: shulou.com Published: 2022-06-01 05:47:50 10月19日 Update

This article mainly explains "what is the life cycle function of WeChat Mini Programs". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the life cycle function of WeChat Mini Programs"?

Since Mini Program's data was not released when we exited Mini Program, the data still did not become initialized when we clicked on it again.

Solution: declare a variable isClose in Mini Program data data, which defaults to true to determine whether it is opened from the entry. When you click to jump to the page or close Mini Program, it will trigger the OnHide function. In this function, when you judge isClose is true, you will open it after closing. When you jump to the page, set isClose to false first, so that when you trigger the OnHide function, isClose is flase will not execute and enter the page that you jump to. Then the OnUnload function of the page that returns from the page that triggers the jump sets a timer in this function to change isClose to true after 200ms, so that when Mini Program is closed and re-entered, isColse is still true when it is judged to enter the page for the first time.

WeChat Mini Programs's Life cycle function

1.1 listening page loading

OnLoad: function (options) {}

A page is called only once, and you can get the query parameters that are called to open the current page in onLoad.

1.2 the first rendering of the listening page is complete

OnReady: function () {}

A page is called only once, indicating that the page is ready to interact with the view layer.

1.3 Monitoring page display

OnShow: function () {}

It is called every time the page is opened.

1.3 listening pages hidden

OnHide: function () {}

Called when navigateTo or the bottom tab is switched.

1.4 uninstall the listening page

OnUnload: function () {}

Called when redirectTo or navigateBack.

Functions unique to WeChat Mini Programs's app.js

2.1 listening for Mini Program initialization

OnLaunch:function () {}

OnLaunch will be triggered when Mini Program initialization is completed (only once globally)

2.2 error listener function

OnError:function () {}

When a script error occurs on Mini Program, or when the api call fails, onError will be triggered with an error message.

At this point, I believe you have a deeper understanding of "what is the life cycle function of WeChat Mini Programs". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Page program function monitor time cycle periodic function life data error content again method learning proper practical deeper lieutenant general representative information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology Xiaomi MariaDB Docker