当前位置:网站首页>Explain the differences and usage scenarios between created and mounted
Explain the differences and usage scenarios between created and mounted
2022-06-13 04:42:00 【Dax1_】
Lifecycle hook
What is life cycle :
- informally , The life cycle is Vue An instance or component is a series of processes from creation to elimination , The middle nodes are called hooks
Lifecycle diagram :
Browser render pass
Have a deep understanding of the various nodes of the lifecycle , You must understand the browser's rendering process
- structure DOM Trees
- structure css The rule tree , Parse according to the execution order js file .
- Build the render tree Render Tree
- Render tree layout layout
- Rendering tree drawing
English class
Since we are analyzing created and mounted hook , Words are also helpful to our understanding
- created: Created
- mounted: Mounted
Browser rendering in the lifecycle
| Life cycle | Whether to obtain dom node | Whether to obtain data | Whether to obtain methods |
|---|---|---|---|
| beforeCreate | no | no | no |
| created | no | yes | yes |
| beforeMount | no | yes | yes |
| mounted | yes | yes | yes |
As you can see from the life cycle diagram at the beginning of this article
createdRender the template as html Pre invocationmountedRender the template as html After the call
beforeCreate Stage
For browsers , The whole rendering process has not yet started or is ready to start , Yes vue Come on , The instance has not been initialized ,data observer and event/watcher It has not been called , At this stage , Yes data、methods Or the call of the document node can't get the correct data now .
created Stage
For browsers , Render the entire HTML When the document ,dom node 、css Rule tree and js After the file is parsed , But no access to the browser render The process , The above resources are not yet mounted on the page , That is to say vue Corresponding in the life cycle created Stage , The instance has been initialized , But it hasn't been mounted to $el On , So we can't get the corresponding node , But at this time, we can get vue in data And methods Of the data in
beforeMount Stage
Actually with created The stages are similar , The node is not mounted , But you can still get data And methods Data in .
mounted Stage
For browsers , It's done dom And css Rule tree render, And finish to render tree It was laid out , And the browser receives this instruction , Call the paint() Show... On the screen , And for vue Come on , stay mounted Stage ,vue Of template Successfully mounted in $el in , At this point, a complete page can be displayed in the browser , So at this stage , The node can be called ( On this point , In the author's test , stay mounted Method and then run, You can still see the whole page in the browser ).
Use scenarios
Through the browser's rendering process , It can be concluded that created and mounted Usage scenarios of
- created: Usually used for Initialize some attribute values , for example data Data in , And then render to the view .
- mounted: Usually after the initialization page is completed , Yes html Of dom The node performs the required operations .
therefore , stay created in , It's impossible DOM Operation of the , and mounted You can get all the rendered attribute values .
Reference documents
[ Lifecycle hook | Vue.js (vuejs.org)]
Vue In the life cycle mounted and created The difference between
边栏推荐
猜你喜欢

Redis

Read paper 20 together: spatiotemporal prediction of PM2.5 concentration by idw-blstm under different time granularity

【Try to Hack】upload-labs通关(暂时写到12关)

2022 ICML | Pocket2Mol: Efficient Molecular Sampling Based on 3D Protein Pockets

C盘无损移动文件

PowerShell: because running scripts is prohibited on this system, the solution

Analyse du principe de mise en œuvre d'un éditeur de texte open source markdown - to - rich

Colab tutorial (super detailed version) and colab pro/pro+ evaluation
![[LeetCode]-二分查找](/img/7f/7d1f616c491c6fb0be93f591da6df1.png)
[LeetCode]-二分查找

Crawler scrapy framework learning 1
随机推荐
PowerShell plus domain add computer module
Idea Download
记录一次排查问题的经过——视频通话无法接起
php开发14 友情链接模块的编写
Get verification code
Time format method on the official demo of uniapp
The problem that ionic3 cannot automatically install the APK package
The differences between the four startup modes of activity and the applicable scenarios and the setting methods of the two startup modes
E - Lucky Numbers
Clear timer failure
【剑指Offer】面试题25.合并两个有序的链表
2022 chlorination process operation certificate examination question bank and simulation examination
PHP security development 15 user password modification module
2022 question bank and answers for operation certificate examination of safety production management personnel in road transport enterprises
Go/golang connection to database
Go scheduled task cron package usage
CTFSHOW SQL注入篇(231-253)
php安全开发15用户密码修改模块
Applet - uniapp realizes the functions of two-dimensional code picture pop-up and picture saving
Cesium:cesiumlab makes image slices and loads slices