VUE - Supplementary knowledge of componentization (life cycle)

insert image description here

Life cycle (important)

insert image description here
It is enough to understand the life cycle of this text version, the main thing is to know what the whole life cycle process looks like
insert image description here
insert image description here

Teacher coderwhy wrote all the functions of the life cycle by hand, and demonstrated it, it is still very clear, you can look at the code.
It is best to write all the functions of the life cycle by yourself, which will be clearer

ref: get the actual DOM (grasp)

If we need to get the native DOM in some development scenarios, we can’t use queryselector. Vue provides the ref attribute to help us get the native DOM. We just need to write ref="name" on the corresponding element . You can get the corresponding native dom through $refs.name!
insert image description here

If it is written on the component, then **$refs. The name gets the component object**
Here is a concept:
a component is actually a class ref in the component is the instantiation generated by this component (class) object. Vue uses objects to implement functions similar to classes in practice, but react is different. React uses classes.

In fact, a component is a class. When a child component is called in a parent component, an instantiated object is created by using the child component template, and different parameters are passed in to obtain different instantiated objects. Isn’t this the same as creating different objects for a class?
are common design patterns

ref gets the component instance, and can directly call the method in the component.
insert image description here
Multiple roots get the first node node,
so it is best to have only one root in a component in development, and put other things in the root, so as to maintain the most convenient time

insert image description here

insert image description here

Dynamic components (component)

Although the teacher wrote to understand, but I think it is quite useful.

In addition to using logic judgments such as v-if and v-else-if, dynamic display components can also use dynamic components
insert image description here

insert image description here
insert image description here
:is - dynamic binding is='component name' case-insensitive
insert image description here
dynamic components can also implement all functions of passing in parameters and listening to events
insert image description here


keep-alive (understanding)

Generally speaking, when we switch components, the components will be destroyed. Using the life cycle function here can also clearly see that the home is indeed destroyed after being switched.
insert image description here
If the switching is more frequent, the overhead will be relatively large, and the previously saved data will disappear, and will disappear when switching back again. So at this time, it is very necessary to keep-alive
insert image description here
to essentially make a cache.
insert image description here
The name of the include is a custom name written in the subcomponent, not the component name, but it is more convenient to write the component name when it can be written
insert image description here

insert image description here
insert image description here

webpack subpackage processing (understanding)

insert image description here

The import function allows webpack to perform subpackage processing, because it returns a promise

asynchronous component

To package a certain component separately, it is required to put a component object in the component, so use the defineAsyncCompoonent provided by vue, which returns an asynchronous component. Use this function to import the component, and this component will be packaged separately when packaging
insert image description here
. a js file
insert image description here

insert image description here

insert image description here

Use v-model on components (advanced usage)

Some things may not be used in development all the time. These are advanced usages. The most important thing is to master the basics.
There is nothing complicated, it is still two-way binding in essence, but because it is data binding between components, communication between components is needed, and some communication between components needs to be written on the sub-components bound by v-model Things, such as props and emits,
insert image description here
insert image description here
the default bound parameter name and event name of v-model is modelValue,
where @input means that the bound input event is triggered when the input table has input input, just like @click is bound to click event.
v-model does two things, one is to pass parameters, the other is to listen to events, which is similar to the input before, but now it is communication between components, so there are still some things such as emits, etc. in the components that use v-model write well!

bind multiple properties

Change the default event name and parameter name, you can use v-model to bind multiple attributes
insert image description here

Mixin (understand)

This is used more in vue2, but it is less used in vue3
because although mixins provide this kind of state reuse capability, it has too many disadvantages . So the hooks were introduced later
Disadvantage 1: Untraceable methods and attributes (because it has the principle of merging)
Disadvantage 2: Overwriting, same name

How to use: mixins: [sayHelloMixin] insert image description hereinsert image description here
insert image description here
global use is less
insert image description here

Summarize

The nested use of components is very important.
Child-to-father and father-to-son are very important. Exercises must be done. Slots must
also be known and must be mastered .
Scope slot understanding
Life cycle is very important, especially created and mounted
insert image description here

Guess you like

Origin blog.csdn.net/Tommy__li/article/details/128321421