An in-depth analysis of the life cycle of uni-app
This article brings you relevant knowledge about the uniapp declaration cycle. It mainly introduces issues related to the application declaration cycle, page life cycle and component life cycle. I hope it will be helpful to everyone.
uniapp tutorial"
Application life cycleuni-app Support the following application life cycle functions:
Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Triggered when | uni-app initialization is completed (only triggered once globally)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
When | uni-app starts, or enters the foreground from the background to display
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
When | uni-app enters the background from the foreground
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Triggered when | uni-app reports an error
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Yes | nvue To monitor the data sent by the page, please refer to nvue communicating with vue
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The unhandled Promise rejection event listening function (2.8. 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The page does not have a listening function | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Listening to system theme changes |
Function name | Description | Platform difference description | Minimum version | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
onInit | Listen to the page initialization, its parameters are the same as the onLoad parameters, which are the data passed on the previous page. The parameter type is Object (used for page parameter transfer), and the triggering time is earlier than onLoad | Baidu Mini Program | 3.1.0 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onLoad | Monitor page loading, the parameter is the data passed on the previous page, the parameter type is Object (used for page parameters), refer to the example |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
onShow | Listen to the page display. Triggered every time the page appears on the screen, including returning from the lower-level page point to reveal the current page | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The initial rendering of the monitoring page is completed. Note that if the rendering speed is fast, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
## will be triggered before the page entry animation is completed. #Listen page hide | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Listen page unload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
App, WeChat applet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
##onTabItemTap |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
WeChat Mini Program, QQ Mini Program, Alipay Mini Program, Baidu Mini Program, H5, App |
onShareAppMessage |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
WeChat applet, QQ applet, Alipay applet, Byte applet, Feishu applet, Kuaishou applet |
onPageScroll |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
nvue does not support it yet |
onNavigationBarButtonTap |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
App, H5 |
onBackPress |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
app, H5, Alipay applet | ##onNavigationBarSearchInputChanged | Monitor the input content of the native title bar search input box Change event|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1.6.0 | onNavigationBarSearchInputConfirmed | Listen to the native title bar search input box search event, user Fired when the "Search" button on the soft keyboard is clicked. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1.6.0 | onNavigationBarSearchInputClicked | Listen to the native title bar search input box click event (pages.json It will be triggered only when the searchInput configuration in disabled is true) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1.6.0 | onShareTimeline | Listening The user clicks the upper right corner to forward to the circle of friends | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2.8.1 | onAddToFavorites | Monitors the user’s click on the upper right corner Collection | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2.8.1 |
Attribute | Type | Description |
---|---|---|
Number | The distance the page has been scrolled in the vertical direction (unit px) |
Note
- onPageScroll Do not write js with complex interactions in
, such as frequent page modifications. Because this life cycle is triggered in the rendering layer, on the non-h5 side, js is executed in the logic layer, and there is a loss in communication between the two layers. If the data exchange between the two layers is frequently triggered during the scrolling process, it may cause lag.
If you want to achieve a transparent gradient of the title bar during scrolling, under App and H5, you can configure the type under titleNView in pages.json to be transparent, for reference. - If you need to scroll the ceiling to fix certain elements, it is recommended to use CSS sticky layout and refer to the plug-in market. There are also other js-implemented ceiling plug-ins in the plug-in market, but their performance is not good. You can search for them by yourself when needed.
- In App, WeChat applet, and H5, you can also use wxs to monitor scrolling, for reference; in app-nvue, you can use bindingx to monitor scrolling, for reference.
- onBackPress
cannot be used on
async, which will result in the failure to prevent the default return of
onPageScroll : function(e) { //nvue暂不支持滚动监听,可用bindingx代替 console.log("滚动距离为:" + e.scrollTop); },
onTabItemTap returned json Object description:
Type | Description | |
---|---|---|
String | The serial number of the clicked tabItem, starting from 0 | |
String | The page path of the clicked tabItem | |
String | The button text of the clicked tabItem |
Note
- onTabItemTap is often used to click on the current tabitem, scroll or refresh the current page. If you click on different tabitem, page switching will definitely be triggered.
- If you want to click a tabitem on the App side without jumping to the page, you cannot use onTabItemTap. You can use plus.nativeObj.view to place a block to cover the original tabitem and intercept the click event.
- Alipay mini program platform onTabItemTap is triggered after clicking on a non-current tabitem, so it cannot be used to implement the operation of clicking back to the top
onTabItemTap : function(e) { console.log(e); // e的返回格式为json对象: {"index":0,"text":"首页","pagePath":"pages/index/index"} },
onNavigationBarButtonTap Parameter description :
Type | Description | |
---|---|---|
Number | The subscript of the native title bar button array |
onBackPress Callback parameter object description:
Type | Description | ||
---|---|---|---|
String | The source of triggering the return behavior: 'backbutton' - the navigation bar button in the upper left corner and the Android return key; 'navigateBack' - the uni.navigateBack() method. | Alipay applet does not support returning this field |
函数名 | 说明 | 平台差异说明 | 最低版本 |
---|---|---|---|
beforeCreate | 在实例初始化之后被调用。详见 | ||
created | 在实例创建完成后被立即调用。详见 | ||
beforeMount | 在挂载开始之前被调用。详见 | ||
mounted | 挂载到实例上去之后调用。详见 注意:此处并不能确定子组件被全部挂载,如果需要子组件完全挂载之后在执行操作可以使用$nextTick Vue官方文档 |
||
beforeUpdate | 数据更新时调用,发生在虚拟 DOM 打补丁之前。详见 | 仅H5平台支持 | |
updated | 由于数据更改导致的虚拟 DOM 重新渲染和打补丁,在这之后会调用该钩子。详见 | 仅H5平台支持 | |
beforeDestroy | 实例销毁之前调用。在这一步,实例仍然完全可用。详见 | ||
destroyed | Vue 实例销毁后调用。调用后,Vue 实例指示的所有东西都会解绑定,所有的事件监听器会被移除,所有的子实例也会被销毁。详见 |
推荐:《uniapp教程》
The above is the detailed content of An in-depth analysis of the life cycle of uni-app. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Steps to launch UniApp project preview in WebStorm: Install UniApp Development Tools plugin Connect to device settings WebSocket launch preview

Generally speaking, uni-app is better when complex native functions are needed; MUI is better when simple or highly customized interfaces are needed. In addition, uni-app has: 1. Vue.js/JavaScript support; 2. Rich native components/API; 3. Good ecosystem. The disadvantages are: 1. Performance issues; 2. Difficulty in customizing the interface. MUI has: 1. Material Design support; 2. High flexibility; 3. Extensive component/theme library. The disadvantages are: 1. CSS dependency; 2. Does not provide native components; 3. Small ecosystem.

uniapp development requires the following foundations: front-end technology (HTML, CSS, JavaScript) mobile development knowledge (iOS and Android platforms) Node.js other foundations (version control tools, IDE, mobile development simulator or real machine debugging experience)

UniApp has many conveniences as a cross-platform development framework, but its shortcomings are also obvious: performance is limited by the hybrid development mode, resulting in poor opening speed, page rendering, and interactive response. The ecosystem is imperfect and there are few components and libraries in specific fields, which limits creativity and the realization of complex functions. Compatibility issues on different platforms are prone to style differences and inconsistent API support. The security mechanism of WebView is different from native applications, which may reduce application security. Application releases and updates that support multiple platforms at the same time require multiple compilations and packages, increasing development and maintenance costs.

When choosing between UniApp and native development, you should consider development cost, performance, user experience, and flexibility. The advantages of UniApp are cross-platform development, rapid iteration, easy learning and built-in plug-ins, while native development is superior in performance, stability, native experience and scalability. Weigh the pros and cons based on specific project needs. UniApp is suitable for beginners, and native development is suitable for complex applications that pursue high performance and seamless experience.

UniApp is based on Vue.js, and Flutter is based on Dart. Both support cross-platform development. UniApp provides rich components and easy development, but its performance is limited by WebView; Flutter uses a native rendering engine, which has excellent performance but is more difficult to develop. UniApp has an active Chinese community, and Flutter has a large and global community. UniApp is suitable for scenarios with rapid development and low performance requirements; Flutter is suitable for complex applications with high customization and high performance.

Recommended component library for uniapp to develop small programs: uni-ui: Officially produced by uni, it provides basic and business components. vant-weapp: Produced by Bytedance, with a simple and beautiful UI design. taro-ui: produced by JD.com and developed based on the Taro framework. fish-design: Produced by Baidu, using Material Design design style. naive-ui: Produced by Youzan, modern UI design, lightweight and easy to customize.
