Home >Web Front-end >JS Tutorial >How to understand the life cycle (hook function) in vue
The Vue life cycle (hook function) represents the process from creation to destruction of the Vue instance object. The Vue life cycle hook function is actually the option of the Vue instance. The values of these options are all functions, which represent the various stages of the instance's life from birth to death. As long as this stage is reached, it will be automatically triggered.
[Recommended course: Vue Tutorial]
What does the life cycle of Vue refer to?
In layman's terms, the life cycle of Vue means that after the web page we write with Vue runs in the browser, the code we write must be executed in memory. For example, we all write var vm = new Vue();
, which means new creates a Vue instance. From the creation of this instance until the instance dies when we close the browser, during this period of time, what does the Vue framework do, what does the Vue instance do, what is done first, what is done later, and what is the relationship between this series of things? Yes, this is the life cycle of Vue.
Vue’s life cycle is divided into three phases: creation phase, running phase, and destruction phase.
In the picture, I have marked each part of the life cycle and made some necessary explanations.
Process explanation (12 steps correspond to the operations in the picture):
1. Generate a Vue instance and execute the hook function beforeCreate( ). [Before instance creation]
2. Initialize the instance.
3. Mount the instance members to the view model and execute the hook function created(). [After the instance is created]
4. Determine whether there is an el object [the el object is used to indicate which area the view we control is].
5. If there is an el object, determine whether a template is used.
6. If a template is used, follow the method of compiling the template. If not, render the view area controlled by el as a template. Execute the hook function beforeMount(). [Before the instance is mounted]
7. Replace the original el view area with the changed new el view area. Execute the hook function Mounted() [after the instance is mounted].
8. Enter the running phase. The running phase is to perform some operations and execute the hook function beforeUpdate(). [Before data update]
9. After the operation is completed, render the data to the page and execute the hook function updated(). [After data update]
10. Enter the destruction phase and execute the hook function beforeDestroy() [Before instance destruction]
11. Perform destruction and disassemble the monitor, subcomponent and event listener .
12. The destruction is completed and the hook function destroyed() is executed. [After instance destruction]
The hook functions in the life cycle are events that Vue must execute during its life cycle. These events are actually functions.
Of course these events allow us programmers to write code so that when Vue's life cycle reaches this point, we can perform the operations we want.
The six hook functions in the creation phase and destruction phase of an instance are always executed once. Once executed, it will not be executed again.
Mentioned in the figure: We can access the instance members we defined only after the init Events are executed in the Vue life cycle, and this point is also the earliest point where the instance members can be accessed. To verify this, let's look at a piece of code.
<body> <div id="app"></div> //这里的路径为本机上的vue.js路径 <script src="./lib/vue.js"></script> <script> var vm = new Vue({ el : '#app', data : { msg : '我是初始值' }, methods : { show : function(){ console.log(this.msg); } }, beforeCreate(){ console.log(this.msg); }, created(){ console.log(this.msg); } }); </script> </body>
The result is as shown in the figure:
You can see that during beforeCreate(), we output undefined, and after created(), we output The value of msg.
This shows that the instance members of Vue are mounted on our vm only after they are created, so you can access our instance members by accessing them after they are created.
The above is the detailed content of How to understand the life cycle (hook function) in vue. For more information, please follow other related articles on the PHP Chinese website!