This article mainly introduces the discussion of Vue's loading order, introducing the loading order in detail and how to judge that all sub-components are loaded. The editor thinks it’s pretty good, so I’d like to share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
In Vuejs version 1.0, if parent and child components cooperate, their life cycle execution has the following characteristics:
1. Created is always created first, then the child
The life cycle function created is always executed in the order from parent to child, but the brothers do not strictly follow this order. It is estimated that an asynchronous function is used. Not only that, the insertion order of child components in the parent component is also random. Yes, there is no special rule. Assume that the reference order of child components is as follows:
<p> <child-c1></child-c1> <child-c2></child-c2> </p>
If $children reference is used to obtain all child components, then "child-c1" is not always in the first position, as follows:
// 大多数时候判断会失败 if(this.$children[0] === this.$refs.child1) { // 这里的代码很可能得不到执行机会 }
2. The order of ready is even more confusing.
According to my estimation, the order of ready for parent and child components should be first son and then parent, so as to ensure that the component is fully loaded. However, judging from practical examples, ready is completely There is no order. Sometimes the parent component is loaded first, and sometimes the child component is loaded first. Therefore, in programming practice, you must not rely on their loading order.
3. Conclusion
In practice, if you need to ensure that components are loaded in sequence, you must not rely on the life order of components, nor the ready life cycle of parent and child components.
How to judge that all sub-components have been loaded
When using parent-child components together, especially when configuration information is separated from business information, we often need to After the sub-component is loaded, the related services of the parent component are executed. Take the following component structure as an example:
<jq-grid> <jq-col></jq-col> <jq-col></jq-col> <jq-col> </jq-col> </jq-grid>
In the above example, we need to combine the configuration information of the sub-components. Therefore, only all sub-components Only when the components are loaded can the combined configuration information be accurate and complete.
So, if you directly use the mounted event (ready event in 1.0), you will definitely get incorrect results. In order to solve this problem, we might as well create the following data structure and method:
props : { colSize : { type : Number, default : 1 } } data () { return { // 用于获取所有的子组件配置信息 colModel : [], readySize : 0 } }, methods : { /** * 由子组件在加载完成时调用 */ addColModel () { this.readySize ++ // 检查进度是否设置的colSize一致 if(this.readySize == this.colSize) { // 这时候所有的子组件已加载完成 } } }
By setting the configuration item "colSize" and checking the method "addColModel" called by the sub-component, we can determine the moment when all sub-components are loaded (the parent component may or may not be loaded at this time), so as to perform the necessary Configuration information sorting operation.
But in actual applications, we found that if the number of sub-components is large, another phenomenon will occur. The loading order of sub-components will occur randomly. If we want to ensure that the loading order is consistent with the configuration order, we can Add the "order" attribute to the subcomponent, as follows. The latest configuration content is as follows:
<jq-grid> <jq-col></jq-col> <jq-col></jq-col> <jq-col></jq-col> </jq-grid>
After such processing, we can sort all subcomponents after they are loaded, as follows:
// 由于Vue无法确定子元素的加载顺序,必须手动指定order this.colModel.sort((a, b) => a.order - b.order)
Conclusion
By manually adding auxiliary variables and methods, you can obtain the time when the sub-component is loaded, so as to perform integrated operations.
Related recommendations:
Detailed explanation of the loading sequence and execution principle of high-performance javascript
A brief analysis of CSS loading and loading Sequence
html, css and js file loading sequence and execution
The above is the detailed content of Discussion on Vue's loading order examples. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software