1. Cold start hot start (update mechanism)

Small program first submitted a version, but found some problems need to be updated in time, this time to release a new version. How to get users to update their own applets to the latest version?

I. Operation mechanism

There are two cases of small program start, one is “cold start”, one is “hot start”. If the user has opened a small program, and then in a certain period of time (five minutes) to open the small program again, at this time no need to restart, just switch to the background state of the small program to the foreground, this process is hot start; Cold start refers to the situation that the user opens it for the first time or the mini program is actively destroyed by wechat and opened again. In this case, the mini program needs to be reloaded and started.

When the mini program enters the background, the client will maintain the running state for a period of time. After a certain period of time (5 minutes at present), the client will be actively destroyed by wechat. After receiving more than two system memory alarms in a short period of time (5s), the mini program will be destroyed

Update mechanism

If a new version is found during cold startup, the system asynchronously downloads the code package of the new version and starts it with the local package of the client. That is, the new version of the small program will be applied only after the next cold startup. If you need to apply the latest version immediately, you can use the Wx.getupDatemanager API to do so.

Update the API

Wx.getupdatemanager () uses this interface to know if the new version applets are available, if the new version is ready to download, and the ability to apply the new version.

// Get applet update mechanism compatibility
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        // Request a callback for the new version information
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: 'Update Prompt'.content: 'The new version is ready. Do you want to restart the application? '.success: function (res) {
                if (res.confirm) {
                  // The new version has been downloaded. Call applyUpdate to apply the new version and restart
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // Failed to download the new version
            wx.showModal({
              title: 'There is a new version.'.content: 'New version has been online ~, please delete the current small program, search again open yo ~',})})}})}else {
      // If you want users to experiment with your applet on the latest version of the client, you can do this
      wx.showModal({
        title: 'tip'.content: 'The current wechat version is too early to use this function. Please upgrade to the latest wechat version and try again. '})}Copy the code

I don’t know where to put this code, I looked up a lot of information when writing this code, and finally put it in the onLaunch method of app.js. The first release will not take effect, the next version will take effect.

Corresponding methods:

Cold start: The method called in this is called when the applet is created, i.e., cold start

onLoad(){},Copy the code

Hot start: Methods called here are called on both cold and hot starts

onShow(){
    this.initPage();
},
Copy the code

In addition: app.js is to support the onShow event execution order onLoad->onShow

Related links: blog.csdn.net/qq_41619796…

Relevant link: blog.csdn.net/gwdgwd123/a…

2. Life cycle

  • Open the applet -> app.onlaunch -> app.onshow -> page.onload -> page.onshow -> page.onReady

  • Go to the Next Page -> page.onhide -> Next. OnLoad -> Next. OnShow -> Next

  • Return to the previous Page -> Next. OnUnload -> Page.onshow

  • Leave the applet -> app.onhide

  • Go again ->app not destroyed -> app.onshow otherwise start from scratch

Note: The Tabbar page is not destroyed after initialization, that is, the onLoad function is executed only once

  • Trigger only once, usually for initialization operations

    OnLaunch: Initializes global data, beware of asynchrony

    OnLoad: initializes page data

    OnReady: indicates that the page is ready, and changes to the interface content are best placed here, such as wx.setNavigationBarTitle

    OnUnload: Clears timers because script logic for all pages runs on the same JsCore thread

  • Triggered more than once, usually to change state

    OnShow: refresh

    Reset onHide:

3. Differences between applets and H5

4. Small program user login architecture design

5. Custom components of wechat applets

6. What pitfalls did you encounter in wechat mini programs and how did you solve them?

  • Canvas drawing failed due to wechat base library upgrade. Procedure
  • NavigateToMiniProgram jump oppo black screen, compatibility problems
  • Wechat PC version does not support several apis, so you need to limit the functions of small programs on the PC side

7. Whether there is a way for wechat applet to jump to other programs

Links to related articles:

Juejin. Cn/post / 684490…