亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
1. Life cycle
2. Life cycle function
Home WeChat Applet Mini Program Development Let's talk about the life cycle (function) in WeChat applet

Let's talk about the life cycle (function) in WeChat applet

Nov 01, 2021 am 10:24 AM
Applets WeChat life cycle

This article will take you to take a look at the life cycle in the WeChat applet, what life cycle functions are there, and talk about its triggering time and what it does. I hope it will be helpful to everyone!

Let's talk about the life cycle (function) in WeChat applet

1. Life cycle

1. What is life cycle?

Life Cycle(Life Cycle) refers to the entire stage of an object from creation-> running-> destruction, emphasizing a time period

2. The life cycle of the mini program

  • Startup of the mini program represents the life cycle Start
  • Close of the applet, indicating the end of the life cycle
  • The process of running the applet in the middle is the life of the applet Cycle

3. Mini program life cycle classification

  • Application life cycle Specifically refers to the process of mini program starting --> running --> destruction

  • Page life cycle Specifically refers to the loading--> rendering--> destruction process of each page in the mini program

  • Note: The life cycle range of the page is small , the life cycle range of the application is larger

Lets talk about the life cycle (function) in WeChat applet

2. Life cycle function

1. What is a life cycle function?

  • The built-in functions provided by the mini program framework will be automatically executed in sequence along with the life cycle

  • Life cycle The role of the function:

    • Allows programmers to perform certain operations at specific life cycle time points
    • For example, when the page is just loaded, in the life cycle function Automatically initiate a data request to obtain the data of the current page
  • Note: The life cycle emphasizes the time period, and the life cycle function emphasizes the time point.

2. Application life cycle function

  • app.js is the entry file for mini program execution. The App() function must be called in app.js and can only be called once. Among them, the App() function is used to register and execute the applet.

  • App(Object) function receives an Object Parameters, you can specify the life cycle function of the applet through this Object parameter

  • Code in app.js

The code is as follows (example):

App({

  /**
   * 當(dāng)小程序初始化完成時(shí),會(huì)觸發(fā) onLaunch(全局只觸發(fā)一次)
   */
  onLaunch: function () { },

  /**
   * 當(dāng)小程序啟動(dòng),或從后臺(tái)進(jìn)入前臺(tái)顯示,會(huì)觸發(fā) onShow
   */
  onShow: function (options) { },

  /**
   * 當(dāng)小程序從前臺(tái)進(jìn)入后臺(tái),會(huì)觸發(fā) onHide
   */
  onHide: function () { },

  /**
   * 當(dāng)小程序發(fā)生腳本錯(cuò)誤,或者 api 調(diào)用失敗時(shí),會(huì)觸發(fā) onError 并帶上錯(cuò)誤信息
   */
  onError: function (msg) { }
})

3. Page life cycle

  • Each mini program page must have its own .js file, and must call the Page() function, otherwise an error will be reported. Among them, the Page() function is used to register the mini program page.

  • Page(Object) function receives an Object parameter. , you can specify the life cycle function of the page through this Object parameter

  • page.js

The code is as follows (example):

//index.js
//獲取應(yīng)用實(shí)例
const app = getApp()

Page({

  /**
   * 頁(yè)面的初始數(shù)據(jù)
   */
  data: { },

  /**
   * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
   */
  onLoad: function (options) { },

  /**
   * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
   */
  onShow: function () { },

  /**
   * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏
   */
  onHide: function () { },

  /**
   * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載
   */
  onUnload: function () { },

  /**
   * 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶下拉動(dòng)作
   */
  onPullDownRefresh: function () { },

  /**
   * 頁(yè)面上拉觸底事件的處理函數(shù)
   */
  onReachBottom: function () { },

  /**
   * 用戶點(diǎn)擊右上角分享
   */
  onShareAppMessage: function () { }
})

4. The entire life cycle of components

What are the life cycles of components? When are they different?

Life cycleParametersDescription
createdNoneExecuted when the component instance is just created
attachedNoneExecuted when the component instance enters the page node tree
readyNoneExecuted after the component is laid out in the view layer
movedNone Executed when the component instance is moved to another location in the node tree
detachedNoneExecuted when the component instance is removed from the page node tree
errorObject ErrorEvery Executed when a component method throws an error

5. 組件主要的生命周期函數(shù)

data在哪個(gè)生命周期中初始化完畢?

組件的生命周期,指的是組件自身的一些函數(shù),這些函數(shù)在特殊的時(shí)間點(diǎn)或遇到一些特殊的框架事件時(shí)被自動(dòng)觸發(fā)。

最重要的生命周期是 created, attached, detached ,包含一個(gè)組件實(shí)例生命流程的最主要時(shí)間點(diǎn)。

  • 組件實(shí)例剛剛被創(chuàng)建好時(shí), created 生命周期被觸發(fā)

    • 此時(shí)還不能調(diào)用 setData
    • 通常情況下,這個(gè)生命周期只應(yīng)該用于給組件 this 添加一些自定義屬性字段
  • 在組件完全初始化完畢、進(jìn)入頁(yè)面節(jié)點(diǎn)樹(shù)后, attached 生命周期被觸發(fā)

    • this.data 已被初始化完畢
    • 這個(gè)生命周期很有用,絕大多數(shù)初始化工作可以在這個(gè)時(shí)機(jī)進(jìn)行
  • 在組件離開(kāi)頁(yè)面節(jié)點(diǎn)樹(shù)后, detached 生命周期被觸發(fā)

    • 退出一個(gè)頁(yè)面時(shí),會(huì)觸發(fā)頁(yè)面內(nèi)每個(gè)自定義組件的detached 生命周期被觸發(fā)
    • 如果組件還在頁(yè)面節(jié)點(diǎn)樹(shù)中,則 detached 會(huì)被觸發(fā)。
    • 此時(shí)適合做一些清理性質(zhì)的工作

6. lifetimes 節(jié)點(diǎn)

同時(shí)以兩種方式聲明生命周期函數(shù),會(huì)執(zhí)行哪個(gè)?

生命周期方法可以直接定義在 Component 構(gòu)造器的第一級(jí)參數(shù)中,組件的的生命周期也可以在 lifetimes 字段內(nèi)進(jìn)行聲明(這是推薦的方式,其優(yōu)先級(jí)最高)

lifetimes: {
  attached () {
    console.log('在組件實(shí)例進(jìn)入頁(yè)面節(jié)點(diǎn)樹(shù)')
  },
  detached () {
    console.log('在組件實(shí)例被從頁(yè)面節(jié)點(diǎn)樹(shù)移除')
  }
},

attached () {
  console.log('~~~~~在組件實(shí)例進(jìn)入頁(yè)面節(jié)點(diǎn)樹(shù)')
},
detached () {
  console.log('~~~~~在組件實(shí)例被從頁(yè)面節(jié)點(diǎn)樹(shù)移除')
},

/**
 * 組件的初始數(shù)據(jù)
 */
data: {
  // rgb 的顏色值對(duì)象
  _rgb: {
    r: 0,
    g: 0,
    b: 0
  },
  // 根據(jù) rgb 對(duì)象的三個(gè)屬性,動(dòng)態(tài)計(jì)算 fullColor 的值
  fullColor: '0, 0, 0'
}

更多編程相關(guān)知識(shí),請(qǐng)?jiān)L問(wèn):編程入門(mén)??!

The above is the detailed content of Let's talk about the life cycle (function) in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
TikTok web version entrance login link address https TikTok web version entrance website free TikTok web version entrance login link address https TikTok web version entrance website free May 22, 2025 pm 04:24 PM

The login portal for the Douyin web version is https://www.douyin.com/. The login steps include: 1. Open the browser; 2. Enter the URL https://www.douyin.com/; 3. Click the "Login" button and select the login method; 4. Enter the account password; 5. Complete login. The web version provides functions such as browsing, searching, interaction, uploading videos and personal homepage management, and has advantages such as large-screen experience, multi-tasking, convenient account management and data statistics.

Copy comics (official website entrance)_Copy comics (nba) genuine online reading portal Copy comics (official website entrance)_Copy comics (nba) genuine online reading portal Jun 05, 2025 pm 04:12 PM

Copying comics is undoubtedly a treasure that cannot be missed. Here you can find basketball comics in various styles, from passionate and inspiring competitive stories to relaxed and humorous daily comedy. Whether you want to relive the classics or discover new works, copying comics can meet your needs. Through the authentic online reading portal provided by copy comics, you will bid farewell to the trouble of pirated resources, enjoy a high-definition and smooth reading experience, and can support your favorite comic authors and contribute to the development of authentic comics.

Which is better, uc browser or qq browser? In-depth comparison and evaluation of uc and qq browsers Which is better, uc browser or qq browser? In-depth comparison and evaluation of uc and qq browsers May 22, 2025 pm 08:33 PM

Choosing UC browser or QQ browser depends on your needs: 1. UC browser is suitable for users who pursue fast loading and rich entertainment functions; 2. QQ browser is suitable for users who need stability and seamless connection with Tencent products.

Top 10 AI writing software rankings Recommended Which AI writing software is free Top 10 AI writing software rankings Recommended Which AI writing software is free Jun 04, 2025 pm 03:27 PM

Combining the latest industry trends and multi-dimensional evaluation data in 2025, the following are the top ten comprehensive AI writing software recommendations, covering mainstream scenarios such as general creation, academic research, and commercial marketing, while taking into account Chinese optimization and localization services:

Watch the official page of NIS comics online for free comics. The free entry website of NIS comics login page Watch the official page of NIS comics online for free comics. The free entry website of NIS comics login page Jun 12, 2025 pm 08:18 PM

Nice Comics, an immersive reading experience platform dedicated to creating for comic lovers, brings together a large number of high-quality comic resources at home and abroad. It is not only a comic reading platform, but also a community that connects comic artists and readers and shares comic culture. Through simple and intuitive interface design and powerful search functions, NES Comics allows you to easily find your favorite works and enjoy a smooth and comfortable reading experience. Say goodbye to the long waiting and tedious operations, enter the world of Nice comics immediately and start your comic journey!

Frog Man Online Viewing Entrance Man Frog Man (Web Page Entrance) Watch Online Frog Man Online Viewing Entrance Man Frog Man (Web Page Entrance) Watch Online Jun 12, 2025 pm 08:06 PM

Frogman Comics has become the first choice for many comic lovers with its rich and diverse comic resources and convenient and smooth online reading experience. It is like a vibrant pond, with fresh and interesting stories constantly emerging, waiting for you to discover and explore. Frog Man comics cover a variety of subjects, from passionate adventures to sweet love, from fantasy and science fiction to suspense reasoning, no matter which genre you like, you can find your favorite works here. Its simple and intuitive interface design allows you to easily get started, quickly find the comics you want to read, and immerse yourself in the exciting comic world.

Baozi Comics (Entrance)_ Baozi Comics (New Entrance) 2025 Baozi Comics (Entrance)_ Baozi Comics (New Entrance) 2025 Jun 05, 2025 pm 04:18 PM

Here, you can enjoy the vast ocean of comics and explore works of various themes and styles, from passionate young man comics to delicate and moving girl comics, from suspenseful and brain-burning mystery comics to relaxed and funny daily comics, there is everything, and there is always one that can touch your heartstrings. We not only have a large amount of genuine comic resources, but also constantly introduce and update the latest works to ensure that you can read your favorite comics as soon as possible.

b An latest registered address_How to register b An exchange b An latest registered address_How to register b An exchange May 26, 2025 pm 07:12 PM

The latest official website of 2025b Announce is: https://www.marketwebb.co/zh-CN/join?ref=507720986&type=wenzi; Binance Exchange is a global cryptocurrency exchange that serves 180 countries and regions including North America, Europe, Taiwan, the Middle East, Hong Kong, and Malaysia. It provides more than 600 cryptocurrencies and has 270 million registered users worldwide.

See all articles