preamble
Everyone should be particularly familiar with vue3's responsive APIs, such as theref
、watch
、watchEffect
etc. Normally everyone is in thevue-cli
orvite
These responsive APIs used inside the created vue project, today Ouyang brings you some different ones.Detach the vue project from theUsing vue's responsive API in projects。
Follow the public number: [Front-end Ouyang], give yourself a chance to advance vue
Straight to the code.
Without further ado, let's get right to the code. This one is the simplest of the new ones I've created locallyproject, as shown below:
As you can see from the image above ourThere is only one project dependency:
vue
. And a script command called dev is provided that actually executes within the node environmentDocumentation.
Let's see.file with the following code:
const { ref, watch, watchEffect } = require("vue");
const count = ref(0);
// analog (device, as opposed digital)countValue modification of variables
setInterval(() => {
++;
}, 1000);
watch(count, (newVal) => {
("trigwatch", newVal);
});
watchEffect(
() => {
("trigwatchEffect", );
},
{
flush: "sync",
}
);
In order to labelThe file is in the
environment, so here I'm specifically using the
require
Go ahead and import the vue exportedref
、watch
、watchEffect
These three responsive APIs.
And we also simulated modificationscount
Responsive variable value manipulation usingsetInterval
Every second, letcount
value of+1
。
In a vue project, it's just as easy to use thewatch
cap (a poem)watchEffect
go and listen incount
The value of the variable.
Execute theyarn dev
That is to say, the execution ofnode
, as shown below:
As you can see from the above figure in theThe results of the implementation in are exactly as expected.
Why is it okay to write like that?
That previous example was aproject, we didn't create a vue component and use the responsive API inside of it like we did in the vue project, but instead, we created the responsive API directly in a normal
file uses the responsive API exposed by vue, and the
watch
cap (a poem)watchEffect
The corresponding watch callback is also triggered when the value of the listener changes, so how does this work again?
This is due to the excellent modular design of vue3, who splits the core functionality into multiple independent modules, as shown below:
for examplereactivity
In the module is the core code for responsive,compiler-core
A module is the core code associated with compilation.
And these modules are also distributed separately as npm packages, with the naming convention being@vue+module name
. For examplereactivity
The corresponding npm package for the module is@vue/reactivity
. Pictured below:
Thanks to the modular design, responsive-related APIs and vue components don't have a strong correlation, so we can use them in theThe application goes to use the responsive API directly.
There are three responsive APIs used here, which are:ref
、watch
cap (a poem)watchEffect
. The principle of responsive implementation in vue components is more or less heard of, in fact, in theThe principle of implementation in the project is the same, next we talk about how to implement responsive.
In our democount
is a responsive variable for ref, and when we make a change to thecount
variable triggers a read operation on theget
Interception. When we are interested in thecount
variable will be triggered when a write operation is performed.set
Interception.
In our case usewatch
cap (a poem)watchEffect
The code for this is something like the following:
watch(count, (newVal) => {
("trigwatch", newVal);
});
watchEffect(
() => {
("trigwatchEffect", );
},
{
flush: "sync",
}
);
When the code is first executed into thewatch
cap (a poem)watchEffect
It's a good idea to keep a close eye on thecount
variable performs a read operation, andwatch
cap (a poem)watchEffect
Both pass in a callback function.
as a result of the introduction ofcount
variable has been read, so it triggers theget
Interception. In theget
The interception will include the currentThe watch callback functionCollected as a dependency oncount
in the variables. The collection is also simple, as thecount
A variable is an object, so using the object'sdep
attribute for dependency collection. Because thedep
An attribute is a collection, so multiple dependencies can be collected.
It's here with us.watch
cap (a poem)watchEffect
They're all triggered.count
variable's get intercept, so thewatch
cap (a poem)watchEffect
The callback functions of thecount
The variables were subjected to dependency collection.
When modifyingcount
variable triggers theset
Interception, atset
What is done in Intercept is also simple. Place thecount
The variable collects all the dependencies taken out and executed once. The dependencies collected here arewatch
cap (a poem)watchEffect
callback function, so when thecount
A change in the value of a variable causes thewatch
cap (a poem)watchEffect
of the callback function is re-executed.
This one is the whole flowchart:
As you can see from the flowchart the responsive implementation turns out to be completely independent of the vue component, so we canThe project uses vue's responsive API, which is one of the marvelous things about vue's design.
summarize
This article talks about how we can break away from the vue project and directly add a new project to theThe project uses vue's responsive API, and goes on to talk about how responsive implementations actually rely on the
get
interception for dependency collection.set
Intercept for dependency triggering.
After figuring out how responsive works, we realize that responsive doesn't rely on the vue component at all, so we can use it in theThe project uses vue's responsive API, which is one of the marvelous things about vue's design.
Follow the public number: [Front-end Ouyang], give yourself a chance to advance vue