Get this
In Vue2, this
in each component points to the current component instance, and this
also contains globally mounted things, routes, state management, etc.
However, there is no this
in the Composition API of Vue3. If you want a similar usage, there are two, one is to access the current component instance, and the other is to access the global instance. You can print it out by yourself and take a look.
|
|
Global Registration(Property/Function)
In Vue2, if we want to mount something globally, we usually do it as follows, and then we can get it through this.xxx
in all components.
|
|
But in Vue3 you can’t write like this. Instead, it changes to a global object that can be accessed by all components, which is the object of the global instance mentioned above. For example, make global registration in main.js.
|
|
Call it in other components.
|
|
Template
In Vue2, there can only be one root node, while Vue3 supports multiple root nodes, which everyone knows.
In essence, each component in Vue3 is still a root node, because the DOM tree can only be a tree structure. It’s just that Vue3 added a judgment during the compilation stage. If the current component has more than one root element, it will add a fragment
component to package this multi-root component, which means this component still only has one root node. And the fragment
, like keep-alive
, is an built-in component that will not be rendered.
Get DOM
|
|
Initialization
In Vue2, when you enter the page and then request the interface, or some other initial operations, they are generally placed in created
or mounted
, while in Vue3 beforeCreated
and created
are not used, because setup
is executed before these two, and having these two would be superfluous.
So the content that was used in the beforeCreated / created / beforeMount / mounted
hooks can be directly placed in the setup in Vue3, or in onMounted/onBeforeMount
.
|
|
Unbinding
In Vue2, there are generally two methods for operations such as clearing timers, listeners, etc:
One is to use $once
in conjunction with hook: BeforeDestroy
, which is not supported by Vue3.
The other is to use the beforeDestroy
/ deactivated
hooks. In Vue3, the hook functions are just renamed.
|
|
Ref and Reactive
Both are used to create responsive objects, ref
is commonly used to create basic types, reactive
is typically used to create responsiveness, this is advocated by official, yet not necessarily in reality. Some people use ref
to define arrays, some people define only one reactive
in a component and put all data in it, just like Vue2’s data, and some people use both.
There are two points to note:
If ref
is passed in an referenced type, the internal source code also calls reactive to implement it.
The property returned by ref is used directly in the template, but in JS it needs to be obtained through .value
, as shown below. Because the ref returns a wrapped object.
|
|
Why does ref
have to return a wrapped object? It’s well known that data in Vue2 all return an object.
Because object reference types can be used for proxies or hijacking, if only the basic type is returned, it is stored in the stack, and once execution in the execution stack is completed, it is recovered, with no possibility of adding a proxy or hijacking. Naturally it’s impossible to track subsequent changes, so it has to return an object, so that it can be responsive.
toRef and toRefs
The common point of these two methods is to create responsive references, mainly used to take the properties out of the responsive objects, or destructure the responsive objects, and the destructured property values are still responsive. If you destructure them directly without these two methods, you will lose the responsive effect.
The main advantage is that we can use the direct variable xxx without needing data.xxx
. And when we modify xxx, we are also directly modifying the underlying object’s properties.
The difference between these two: The one with “s” and without “s”, are singular and plural. The meaning is to take one or to take many.
|
|
watch
watch
is used to monitor an existing property, and to do certain operations when changes occur. The following three ways of writing are commonly used in Vue2.
|
|
In Vue3, the listening syntax is much more enriched.
Vue3’s watch
is a function that can receive three parameters, the first parameter is the property to be monitored, the second one is the callback function to receive the new and old values, and the third one is the configuration item.
|
|
Inside the watch callback function, you can accept the third parameter onInvalidate, which
is a function that clears side effects. For the first time, the callback function of the listening (handler) is not going to trigger onInvalidate
, and subsequently, will trigger onInvalidate
by default each time.
In other words, it’s default operation mechanism called before updating, for example, in the following code, when the key triggers an update, it will print 222 first and then print xxx. If you need to call after updating, you can add flush: post in the third configuration of watch.
|
|
The use scenario of onInvalidate
is such as: for example, there are some asynchronous operations in the callback function (handler) of listening, and when triggering watch
again, it can be used to cancel/ignore/reset/initialize some operations of the previous unfinished asynchronous tasks, such as canceling the unfinished request when triggering watch
last time.
watchEffect
In Vue3, in addition to watch
, there is also a watchEffect
. The differences are:
watch
is to listen to one or more values passed in, and it will return new and old values when triggered, and by default it will not execute for the first time.
watchEffect
is a function that is executed immediately, so it will be executed by default for the first time, and there is no need to pass in listening content. It will automatically collect the data sources in the function as dependencies, and it will re-execute the function when the dependencies change. (a bit like computed), and it will not return new and old values.
The timing of side effect clearance and refreshment of side effects are the same, the difference is that in watch
, it will be passed as the third argument of the callback, in watchEffect
it is the first argument of the callback function.
Normally, both will stop listening automatically after the component is destroyed/unmounted
, but there are exceptions, such as asynchronous methods, listeners created in setTimeout need to manually stop listening, and the stop method is as follows.
|
|
Use of watchEffect
:
|
|
If you need to change the configuration item flush to post or sync in watchEffect
, you can directly use the alias, as follows.
|
|
computed
In Vue2, the most common use cases for computed are: mapGetters/mapState
for fetching property from state management, getting property from a URL, conditional judgments, type conversions, etc., and it supports both function and object writing methods.
In Vue3, computed
is no longer an object, but a function. The usage is somewhat similar in general, the first argument of the function is the listener source, used to return the computed
new value. It also supports object writing methods, and the second argument can be used for debugging.
|
|
nextTick
The usage method of nextTick
, excluding the use of this, everything else is exactly the same as Vue2, and there are still three ways.
|
|
mixins and hooks
In Vue2, logic extraction and reuse generally use mixins
, but there are three disadvantages:
There is no independent namespace,
mixins
will cause naming conflicts with the internals of the componentIf you don’t go through the code, you won’t know what’s in the imported
mixins
When multiple
mixins
are introduced, you don’t know which one the mixin you are using comes from
The hooks syntax for logic extraction and reuse in Vue3 is actually just a function that can accept arguments and use the returned values. Or, it can be understood this way: how to write commonly used methods that need to be encapsulated? You can do it just like that in Vue3.
|
|
Regarding how to write more elegant code with hooks, one needs to write more and practice more. It’s not something that can be mastered with a few sentences and lines of code.
Component communication
There are several ways to communicate between components in Vue3:
props + defineProps
defineEmits
defineExpose / ref
useAttrs
v-model (supports multiple)
provide / inject
Vuex / Pinia
multiple v-model
In Vue2, only one v-model
can be written on each component. If the child component does not write a model, it can be received by props by default, and modifications are made through the this.$emit(‘input’)
event.
In Vue3, each component supports writing multiple v-model
, eliminating the need for .sync
and model renaming operations. When writing v-model
, you need to include the name, as follows:
|
|
State Management
The usage of Vuex is basically the same as Vue2. If you are starting from scratch, it’s recommended to use Pinia directly.
router
|
|
CSS Style Penetration
In Vue2, when it’s not possible to modify the style of sub-components or components within a library in scoped
, you can use CSS style penetration. Regardless of whether it’s Less or SASS, you use /deep/ .class {}
for style penetration. However, Vue3 does not support the /deep/
syntax, it has been replaced with :deep(.class)
for style penetration.
|
|
Binding JS Variables with CSS
|
|