Vue —— vue-related interview questions sharing

1. The difference between create and mount

Answer: created: Called before the template is rendered into html, that is, some attribute values ​​are usually initialized, and then rendered into a view.

mounted: Called after the template is rendered into html, usually after the initialization page is completed, and then perform some required operations on the dom node of html

Usually created is used more times, and mounted is usually operated in the use of some plug-ins or components.

2. At what stage is the dom operated (/when can the dom be obtained)

Answer: Before the hook function mounted is called, Vue has already mounted the compiled template to the page, so the DOM can be accessed and operated in mounted

3. When can the data be obtained in the life cycle

Answer: created: the initialization of data data has been completed, but el has not

4. What is the role of the vue life cycle?

Answer: There are multiple event hooks in its life cycle, which makes it easier for us to form good logic when controlling the process of the entire Vue instance.

5. How many stages are there in the vue life cycle?

A: It can be divided into 8 stages in total: before/after creation, before/after loading, before/after update, and before/after destruction.

They are:
    beforeCreate (before creation)
    created (after creation)
    beforeMount (before loading)
    mounted (after loading)
    beforeUpdate (before update)
    updated (after update)
    beforeDestroy (before destruction)
    destroyed (after destruction)

6. Which hooks will be triggered on the first page load?

Answer: The following beforeCreate, created, beforeMount, mounted will be triggered.

7. In which cycle is DOM rendering completed?

Answer: DOM rendering has been completed in mounted.

8. What is the role/significance of each stage (hook function) of Vue?

Answer: Multiple hook functions in the life cycle give users the opportunity to add code at different stages.

9. What is the difference between created and mounted?

Answer: created: when called, the template has not been rendered at this time, and the DOM cannot be manipulated, and it is mainly used to initialize data;
mounted: when called, the template has been rendered into html at this time, and the DOM can be manipulated

10. Briefly describe which scenarios each cycle is suitable for

Answer: beforecreate: You can add a loading event here, which is triggered when the instance is loaded.
created: the event when the initialization is completed is written here. If the loading event ends here, the asynchronous request is also suitable to call mounted here
: mount the element and get it DOM node
updated: If the data is processed uniformly, write the corresponding function here
beforeDestroy: You can make a confirmation box to confirm the stop event
nextTick: Operate the dom immediately after updating the data


Summarize

Read a few questions every day, and you will learn a lot over time. Come on, come on!

おすすめ

転載: blog.csdn.net/Bonsoir777/article/details/128089371